#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Dynamic memory Allocation in c language
1.
2. What is Memory?
Computer memory is any physical device capable of storing
information temporarily or permanently.
Memory is the processes by which information is encoded,
stored and retrieved.
3. Type of Memory Allocation
There are two type of memory allocation.
1) Static memory allocation.
2) Dynamic memory allocation.
Different Between Static and dynamic memory allocation.
• Static memory allocation is allocated the memory at
the compile time. Dynamic at the runtime of
execution of program.
• In static memory can’t be increase while execution of
the program. But in the dynamic memory can
increase while executing the program.
• Static used in array and dynamic used in linked list.
4. What is Dynamic memory
Allocation?
Memory space required can be specified at the time of
execution.
The concept of dynamic memory allocation in c
language enables the C programmer to allocate memory
at runtime. Dynamic memory allocation in c language is
possible by 4 functions of stdlib.h header file.
malloc()
calloc()
realloc()
free()
5. What is Allocation?
An allocation is something that you set aside for use. For
instance if you want to set aside a certain amount of hard
drive space for an application.
Memory allocation is the process of setting aside section
of memory in a program to be used to store variables, and
instances of structure and classes.
.
6. malloc()
The declaration of malloc ( ) function is
Syntax:-
Void*malloc(size_t size)
Example:- int *nums = (int*)malloc(5*sizeof(int));
This is similar to int *nums=new int[5];
Deallocation of using free[] function-
free(nums);
This is special function that are used to assign value to
variable.
New return exact datatype, while malloc() return void
pointer.
To use malloc() you must #include<stdlib.h>
7. malloc() …continue….
On error malloc()return NULL.
If size =0 malloc() also return NULL.
The malloc function allocates a block of size bytes from
the memory heap.
It allows a program to allocates memory as it’s needed
and in the exact , amount needed.
On success malloc() return a pointer to the newely
allocated block of memory.
8. malloc()….. Continue..
char *charpt; /* declare a pointer to char */
charpt
charpt = malloc(10);
10 bytes or chars
charpt
charpt contains the address of the beginning of that
block.
9. calloc()
The calloc() function allocates multiple block of
requested memory.
It returns NULL if memory is not sufficient.
The declaration of calloc ( ) function is –
void *calloc(size_t n items , size_t size)
Example:- ptr = (float*) calloc(25, sizeof(float));
calloc() provided access in c memory heap. which is
available for dynaminc allocation of variable size block
of memory.
10. calloc()… continue..
The name calloc stands for "contiguous allocation".
The only difference between malloc() and calloc() is
that, malloc() allocates single block of memory whereas
calloc() allocates multiple blocks of memory each of
same size and sets all bytes to zero.
Allocates space for an array of elements, initializes them
to zero and then returns a pointer.
11. realloc()
Reallocates the memory occupied by malloc() or calloc()
functions.
Syntax:-
void *realloc(void *ptr, size_t newsize);
Let’s take an example:-
int *ptr;
// allocate memory for 5 integers
ptr = (int*)malloc(5*sizeof(int));
// allocate memory for 6 more integers i.e a total of 11.
ptr = (int*)realloc(ptr, 11*sizeof(int));
13. free()
The C library function void free(void *ptr) deallocates
the memory previously allocated by a call to calloc,
malloc, or realloc.
Syntax:-
void free(void *ptr)
Example:-
Int main(){
char *str;
-----
-----
free(str);
}