1- Two types of dynamic memory allocation one is calloc and other is malloc.View more random threads:
- what is program in C++ May 2011 ?
- Read data from file into arrays in c++ 2011
- But "cin" lines are not producing the results 2011
- how to insert a new node on AVL tree using double rotation?
- i solve the assignment i mean which ms word doc file can i...
- What are Static Data Members in c++ 2011?
- What is the purpose of using classes if we have the...
- when we make codes in Devc++ after press F9 (compiling) the...
- How can we stop a programme at any point in C++?
- latest C++ Books 2011
There are two differences. First is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments.
malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.
calloc() allocates a memory area, the length will be the product of its parameters. calloc fills the memory with ZERO's and returns a pointer to first byte. If it fails to locate enough space it returns a NULL pointer.
Syntax: ptr_var=(cast_type *)calloc(no_of_blocks , size_of_each_block); i.e. ptr_var=(type *)calloc(n,s); malloc() allocates a single block of memory of REQUSTED SIZE and returns a pointer to first byte. If it fails to locate requsted amount of memory it returns a null pointer.
Syntax:
ptr_var=(cast_type *)malloc(Size_in_bytes);
The malloc() function take one argument, which is the number of bytes to allocate, while the calloc() function takes two arguments, one being the number of elements, and the other being the number of bytes to allocate for each of those elements. Also, calloc() initializes the allocated space to zeroes, while malloc() does not.
If a pointer has already been allocated memory using malloc and later this allocated memory needs to be expanded for some reason, the realloc function is used. It will add the consecutive memory spaces to the existing if available, otherwise it will allocate whole new space for the same pointer.
Sponsored Links
There are currently 1 users browsing this thread. (0 members and 1 guests)