The three functions are declared as follows:
Void * realloc (void * PTR, unsigned newsize );
Void * malloc (unsigned size );
Void * calloc (size_t nelem, size_t elsize );
All in the stdlib. H function library
Their return values are all the addresses allocated by the request system. If the request fails, null is returned.
Malloc is used to apply for a new address. The parameter size is the length of the memory space, for example:
Char * P;
P = (char *) malloc (20 );
Calloc is similar to malloc. The elsize parameter is the unit element length of the request address, and nelem is the number of elements, for example:
Char * P;
P = (char *) calloc (20, sizeof (char ));
This example has the same effect as the previous one.
Realloc is to allocate space to a pointer that has allocated an address. The PTR parameter is the original space address, and newsize is the length of the requested address, for example:
Char * P;
P = (char *) malloc (sizeof (char) * 20 );
P = (char *) realloc (p, sizeof (char) * 40 );
Note that the space length here is measured in bytes.
C language standard memory allocation functions: malloc, calloc, realloc, etc.
The difference between malloc and calloc is the difference between 1 block and N block:
Malloc is called in the form of (type *) malloc (size): allocates a continuous area with a length of "size" bytes in the dynamic storage area of the memory, and returns the first address of the region.
Calloc is called in the form of (type *) calloc (n, size): In the dynamic storage area of the memory, allocate n consecutive areas with the length of "size" bytes, each bit is initialized to zero and returns the first address.
The realloc call format is (type *) realloc (* PTR, size): increase the size of the PTR memory to size.
Free is called in the form of free (void * PTR): releases the memory space pointed to by PTR.
C ++ is the new/delete function.
This article is from the csdn blog. For more information, see the source:Http://blog.csdn.net/Mobidogs/archive/2007/04/05/1552732.aspx