The statement of the three functions is:
Void * realloc (void * PTR, unsigned newsize );
Void * malloc (unsigned size );
Void * calloc (size_t numelements, size_t sizeofelement );
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 sizeofelement parameter is the unit element length of the requested address, and numelements 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 parameter PTR 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 Standard memory allocation functions: malloc, calloc, realloc, and free.
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 and return 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.
If the call is successful, both the malloc () and calloc () functions return the first address of the allocated memory space.
The main difference between the malloc () function and calloc () function is that the former Cannot initialize the allocated memory space, while the latter can. If the memory space allocated by the malloc () function has not been used before, each of them may be 0; otherwise, if this part of memory has been allocated, there may be a variety of data. That is to say, when the program using the malloc () function starts (the memory space has not been reassigned), it can be normally carried out, but after a period of time (the memory space has been reassigned) problems may occur.
The calloc () function initializes every bit in the allocated memory space to zero. That is to say, if you allocate memory for elements of the character or Integer type, then these elements will be initialized to 0; if you allocate memory for the elements of the specified needle type, these elements will usually be initialized to a null pointer; if you allocate memory for real data, these elements will be initialized to zero of the floating point type.
Malloc requests the system to allocate a specified size of memory space. The return type is void. Void * Indicates a pointer of unknown type. C, C ++ stipulates that the void * type can be forcibly converted to any other type of pointer.
We can see from the function declaration. There are at least two differences between malloc and new: New returns a pointer of the specified type and can automatically calculate the required size. For example:
Int * P;
P = new int; // The return type is int * (integer pointer) and the allocated size is sizeof (INT );
Or:
Int * Parr;
Parr = new int [100]; // The returned type is int * (integer pointer) and the allocated size is sizeof (INT) * 100;
While malloc must be calculated by the number of bytes and forcibly converted to the actual type of pointer after the return.
Int * P;
P = (int *) malloc (sizeof (INT ));
First, the malloc function returns the void * type. If you write it as P = malloc (sizeof (INT), the program cannot be compiled and an error is returned: "You cannot assign void * to int * type variables ". Therefore, you must use (int *) to forcibly convert the data.
Second, the real parameter of the function is sizeof (INT), used to specify the size required for an integer data. If you write:
Int * P = (int *) malloc (1 );
The code can also be compiled, but in fact only one byte memory space is allocated. When you store an integer in it, three bytes will be left homeless, and you can directly "Live in the neighbor's house "! The result is that all the original data in the memory is cleared.
Malloc can also achieve the effect of new [], and apply for a continuous memory, the method is nothing more than specify the memory size you need.
For example, you want to allocate 100 int-type space:
Int * P = (int *) malloc (sizeof (INT) * 100); // allocate memory space that can be placed in the next 100 integers.
Another difference that cannot be seen directly is that malloc only allocates memory and Cannot initialize the obtained memory. Therefore, the value of a new memory will be random.