C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
Dynamic memory in cTill now in the previous sessions The memory allocation that we have done was static memory allocation. so now till the memory use by any program was fix eg. we can not allocate or deallocate memory of program during the execution of the program. In many programs or application there is not know in advance that how much memory needed by the program at run time. for example we declare an array of integer type with 200 size.
In this example, in the array it is must specify the size of array when array is declare. so in this case size of array will be fix during the run time. now in this case two types of problem may occurs, in the first case the number of values to be store in the array may less then the size of array, So here will be wastage of memory. For example if we want to store 30 values in the above Array then space for 170 values(680 bytes) will be wastage, and in the second case in our program if we want to store more values then the size of Array. For example if we want to store 300 values in the above array it is also problem To resolve these problems we should allocate and deallocate memory at the run time. The process of allocation of memory at the run time is known as Dynamic memory allocation. These allocation and deallocation of memory can be done using library functions that are declare in stdlib.h herder file. These functions allocate and deallocate memory from heap memory area. pointers has the important role in dynamic memory allocation because we can access the dynamic memory only through pointers. malloc Declaration syntax:
This function is used to allocate memory dynamically. in this example the arguments size specifies the number of bytes to be allocated. The function ,malloc() returns a pointer to the first byte of allocated memory.so The returned pointer is of type void. so it is generally used as Here in this example ptr is a pointer variable,and in the argument list specified size is the size in the bytes required to allocated in this example the expression(datatype *) is used to typecast the pointer returned by malloc() let us take example..
from the above example this allocates 10 continues bytes memory space and the address of the first byte stored in the pointer variable that is ptr In this example this space can hold 2 integers and then 2 bytes also remaining free. now allocate memory contains garbage value untill it is not initialize by any value. we can use sizeof operator to make the program portable and more readable
In the example the memory space hold the four integers values. But in this condition if there is no space in heap memory then malloc() returns NULL. so we should always check the value return by malloc(). we can check like this
calloc() Declaration
The calloc() function is also used to allocate memory blocks. It is similar to malloc(). But there are two differences, The first one is that it takes two arguments , the first arguments take number of blocks to be allocated and second argument specifies the size of each block for example
in this above example this allocates the 8 block of memory, and every block contains 4 bytes and starting address is stored in pointer variable ptr. that is type of integer. if wee use here malloc() it would be
The other main difference is between the malloc() and calloc() is that memory allocated by malloc() contains the garbage value and the memory allocate by the calloc() initialize to zero. realloc()
when we want ot increase or decrease the memory size that is allocated by malloc() or calloc(), so the generally realloc() is used to change to size of memory. It changes the size of memory without losing any data. it also called reallocation of memory.In the above example we show how it is declare.It takes two arguments the first argument is pointer variable that points to the memory that is previous allocated by calloc() or malloc() and second argument is the new size for the block take the example with malloc()
in this above statement allocates memory using the specified size and the starting address of the memory is stored in the pointer variable ptr if we want to change the size of this memory block, we use the realloc()
in this statement allocate the memory size with new size bytes. and starting of the address of memory stored in the pointer variable in ptr. on failure realloc() is return NULL and in this case the old memory is not deallocated and it is remaining unchanged. If pointer variable ptr is null, realloc() behaves like malloc() function, that means if ptr is not a pointer return by malloc(), calloc() .
free() so allocated memory by the previous methods malloc(),calloc() and reallock(), It is our responsibility to release that memory so it can be reused. So the function free() is used to release the memory space allocated dynamically. The free() function used to allocated heap memory again this heap can be use by another purpose
In this example ptr is a pointer variable that contains the base address of memory that is already created by malloc() or calloc(). so by free() memory released. once by free() memory location is freed it should not be used again. so note that we should never to try to do free memory that is not allocated by malloc(),calloc() and realloc() Note: when we terminates the programme os(operating system) automatically released the memory, but it is good practice to realased memory or free memory that has been allocated dynamically |