Difference between C language malloc, calloc, and realloc, callocrealloc
The statement of the three functions is:
void* malloc(unsigned size); void* realloc(void* ptr, unsigned newsize); void* calloc(size_t numElements, size_t sizeOfElement);
All are in the stdlib. h function library. Their return values are the addresses allocated by the request system. If the request fails, NULL is returned.
(1) function malloc ()
In the dynamic storage area of the memory, a contiguous area with a length of size bytes is allocated. The parameter size is the length of the memory space required, and the first address of the area is returned.
(2) function calloc ()
Similar to malloc, The sizeOfElement parameter is the length of the unit element of the request address, and numElements is the number of elements, that is, the continuous address space for applying numElements * sizeOfElement bytes in the memory.
(3) function realloc ()
Allocate space to a pointer that has been allocated an address. The ptr parameter is the original space address, and newsize is the length of the requested address.
Differences:
(1) The function malloc Cannot initialize the allocated memory space, while the function calloc 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 re-allocated), it can work normally, but after a period of time (the memory space has been re-allocated) problems may occur.
(2)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, these elements are always initialized to 0; if you allocate memory for pointer elements, these elements are usually initialized to a null pointer; if you allocate memory for real data, these elements will be initialized to zero of the floating point type.
(3)The malloc function applies to 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.
(4)Realloc can expand or contract the space indicated by the given pointer. Whether it is expansion or reduction, the content in the original memory will remain unchanged. of course, for downgrading, the contents of the reduced part will be lost. realloc does not guarantee that the adjusted memory space is the same as the original memory space. instead, the pointer returned by realloc is likely to point to a new address.
(5)Realloc allocates memory from the stack. when a memory space is expanded, realloc () tries to directly obtain additional bytes from the byte following the existing data on the stack. If yes, it is natural that the world is peaceful; if the number of bytes after the data is not enough, the problem arises. The first free block with sufficient size on the stack will be used, and the existing data will be copied to the new location, the old block is put back on the stack. an important information transmitted in this sentence is that the data may be moved.
#include <stdio.h> #include <malloc.h> int main(int argc, char* argv[]) { char *p,*q; p = (char *)malloc(10); q = p; p = (char *)realloc(p,10); printf("p=0x%x/n",p); printf("q=0x%x/n",q); return 0; }
Output Result: After realloc, the memory address remains unchanged.
P = 0x431a70
Q = 0x431a70
Example 2:
#include <stdio.h> #include <malloc.h> int main(int argc, char* argv[]) { char *p,*q; p = (char *)malloc(10); q = p; p = (char *)realloc(p,1000); printf("p=0x%x/n",p); printf("q=0x%x/n",q); return 0; }
Output result: the memory address changes after realloc.
P = 0x351c0
Q = 0x431a70