C++ Programming/Code/Standard C Library/Functions/calloc

From Wikibooks, open books for an open world
Jump to: navigation, search


#include <cstdlib>
void *calloc( size_t num, size_t size);

The function calloc() allocates a block of memory that can store an array of num cells, each with a size size. Every cell of the array is set to value zero.

If the operation fails, calloc() returns "NULL".


     ptr = (float*)calloc(25, sizeof(float));
     /* It would create an array of 25 cells, each one with a size of 4 bytes
        “(sizeof(float))”, all the cells initialized with value 0 */
Related topics
free - malloc - realloc