C Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime.
C provides some functions to achieve these tasks.
What is Dynamic Memory Allocation ?
Dynamic memory allocation is used to obtain and release memory during program execution. Up until this point we reserved memory at compile time using declarations. You have to be careful with dynamic memory allocation. It operates at a low-level, you will often find yourself having to do a certain amount of work to manage the memory it gives you. To use the functions discussed here, you must include the stdlib.h header file.
There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming.
They are:-
- malloc()
- calloc()
- free()
- realloc()
C malloc()
“malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It initializes each block with default garbage value.
To allocate memory use:
void *malloc(size);
- Takes number of bytes to allocate as argument.
- Use sizeof to determine the size of a type.
- Returns pointer of type void *. A void pointer may be assigned to any pointer.
- If no memory available, returns NULL.
e.g.
char *line;
int linelength = 100;
line = (char*)malloc(linelength);
malloc() example
To allocate space for 100 integers:
int *ip;
ip = (int*)malloc(100
*
sizeof(int))
Note we cast the return value to int*.
C calloc()
“calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. It initializes each block with a default value ‘0’.
Similar to malloc(), the main difference is that the values stored in the allocated memory space are zero by default. With malloc(), the allocated memory could have any value.
calloc() requires two arguments - the number of variables you'd like to allocate
memory for and the size of each variable.
void *calloc(size_t nitem, size_t size);
Like malloc(), calloc() will return a void pointer if the memory allocation
was successful, else it'll return a NULL pointer.
calloc() example
float * buffer;
int i;
buffer = (float*)calloc(100,
sizeof(float))realloc
C free()
“free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.
syntax:
free(ptr);
To release allocated memory use:
free()
■ Deallocates memory allocated by malloc().
■ Takes a pointer as an argument.
e.g.
free(newPtr);
Freeing unused memory is a good idea, but it's not mandatory. When your
program exits, any memory which it has allocated but not freed will be
automatically released.
C realloc()
“realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory. re-allocation of memory maintains the already present value and new blocks will be initialized with default garbage value.
If you find you did not allocate enough space use realloc().
You give realloc() a pointer (such as you received from an initial call to
malloc()) and a new size, and realloc does what it can to give you a block of
memory big enough to hold the new size.
int *ip;
ip = (int*)malloc(100 * sizeof(int));
...
/* need twice as much space */
ip = (int*)realloc(ip, 200 * sizeof(int));
Syntax:
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
0 Comments