calloc() - Dynamic Allocation Function
How is calloc() used in C++?
Explanation
calloc() is used to allocate the memory, which will be equal to the value, "num*size". Function returns a pointer to the first byte of the allocated space.If there is no space to allocate, a "NULL" pointer is returned.
Syntax:
void *calloc(size_t num, size_t);
Example :
#include <stdlib.h> #include <stdio.h> int main() { unsigned num; int *ptr; printf("Enter the number to allocate: "); scanf("%d", &num); ptr = (int*)calloc(num, sizeof(int)); if (ptr != NULL) puts("Memory allocation was successful."); return(0); } |
Result :
Enter the number to allocate: 1000
Memory allocation was successful
In the above Dynamic allocation function example the number parameter for the "calloc()" is got as input. Once the memory is allocated a message is displayed.