Malloc Function ( Dynamic Memory Allocation Part - 2 )
Defination
The malloc allocates a block of memory in the heap and a pointer is to be declared at the stack of the memory which would contain the base address of the heap's first memory byte and would permanently point at it. During runtime of the program the memory is accessed not directly but through pointers which have declared in the stack.The address returned is in the form of a void pointer address, which is to be type casted according to convenience of the user. So malloc returns a null pointer.
Malloc function is declared under stdlib.h library. So include this in the program in the section of the header files, to avoid getting a warning or syntax error in certain compilers.
The general syntax of the malloc function is :
< data_type> *pointer;
pointer = ( < type casting * > ) malloc(< byte_size >)
For storing float in the dynamic memory we can use :
float *pointer;
pointer= ( float* )malloc(5*sizeof( float ))
A program using malloc function :
Check out other programs on calloc(), realloc () and free() funtions.
Comments
Post a Comment