Malloc calloc, realloc, callocrealloc
Realloc () function
Prototype: extern void * realloc (void * mem_address, unsigned int newsize );
Syntax: pointer name = (data type *) realloc (name of the pointer to change the memory size, new size ).
Header file: # include <stdlib. h> Some compilers need # include <alloc. h>. You can use the alloc. h header file in TC2.0.
Function: first allocates space according to the size specified by newsize, copies the original data from start to end to the newly allocated memory area, and then releases the memory area specified by the original mem_address, return the first address of the newly allocated memory area. That is, the address of the memory block is re-allocated.
Returned value: If the reallocation succeeds, the pointer pointing to the allocated memory is returned. Otherwise, the NULL pointer is returned.
Note: The data in the original memory remains unchanged. When the memory is no longer used, use the free () function to release the memory block.
Malloc () function
Prototype: extern void * malloc (unsigned int num_bytes );
Header file: malloc can be used in TC2.0. h or alloc. h (Note: alloc. h and malloc. the content of h is completely consistent.) in Visual C ++ 6.0, you can use malloc. h or stdlib. h.
Function: allocate memory blocks of num_bytes bytes.
Return Value: if the allocation is successful, the pointer pointing to the allocated memory is returned. Otherwise, the NULL pointer is returned. When the memory is no longer used, use the free () function to release the memory block.
NOTE: For the prototype of the function, malloc returns a char pointer in the old version. The new ANSIC standard stipulates that the function returns a void pointer, therefore, type conversion is required.
Calloc () function
Calloc is a C function.
Function: allocate n consecutive spaces with a length of size in the dynamic storage area of the memory. The function returns a pointer pointing to the starting address of the allocation. If the allocation fails, NULL is returned.
Differences from malloc:
After the memory is dynamically allocated, calloc automatically initializes the memory space to zero, while malloc does not initialize, and the data in the memory is random junk data.
Usage: void * calloc (unsigned n, unsigned size );
Header file: stdlib. h or malloc. h
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num = 10;
int i;
long *p = (long *)malloc(num * sizeof(long));
long *p1=(long *)calloc(num,sizeof(long));
for (i = 0; i < num; i++)
{
printf("%d\t", p[i]);
}
for (i = 0; i < num; i++)
{
printf("%d\t", p1[i]);
}
printf("内存地址: %p\n~~~~~~~~\n", p);
for (i = 0; i < num; i++) p[i] = i+1;
for (i = 0; i < num; i++) printf("%d\t", p[i]);
printf("\n------------------\n");
num = 4;
p = (long *)realloc(p, num*sizeof(long));
printf("内存地址: %p\n~~~~~~~~\n", p);
for (i = 0; i < num; i++) printf("%d\t", p[i]);
printf("\n------------------\n");
num = 10;
p = (long *)realloc(p, num*sizeof(long));
printf("内存地址: %p\n~~~~~~~~\n", p);
for (i = 0; i < num; i++) printf("%d\t", p[i]);
free(p);
free(p1);
getchar();
return 0;
}
The running result is:
Reprinted from: http://www.360doc.com/content/11/0801/10/449394_137098674.shtml