Friday, 21 June 2013

difference between malloc and callloc

 1: calloc(...) allocates a block of memory for an array of elements of a certain size whereas malloc takes only single argument and     allocated memory in contigious memory location.

2: calloc()   initialized with 0 and the  total number of  memory allocated will be  (number_of_elemets * size) whereas malloc()  initializes the memory location with garbage value;

3: malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not    blocks of memory like calloc(...).

4 malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++.

       char *Pstr;
5:    Code for the malloc
            Pstr = (char*)malloc(sizeof(char)* No Of Element));      

         Code For Calloc
           Pstr =  (char*)calloc(100, sizeof(int));        
since malloc and calloc returns void pointer so we typecast is mandatory

0 comments:

Post a Comment