dynamic memory allocation

1. All operations in C language are based on memory

2. Variables and arrays are aliases of memory. How to allocate these memory is determined by the compiler during compilation. When defining an array, the length of the array must be specified, and the length of the array must be determined at compile time.

need:

While the program is running, it may need to use some additional memory space.

malloc and free are used to perform dynamic memory allocation and deallocation.


1. What malloc allocates is a contiguous piece of memory, in bytes, without any type information

2. free is used to return dynamic memory to the system


Notice:

1. The actual memory allocated by malloc may be slightly more than requested, but this behavior of the compiler cannot be relied on

2. malloc returns NULL when the requested dynamic memory cannot be satisfied

3. When the parameter of free is NULL, the function returns directly


calloc and realloc

1. The parameter of calloc represents the type information of the returned memory

2.calloc will initialize the returned memory to 0

3. realloc is used to modify the size of a previously allocated memory block. After using realloc, its return value should be used. When the first parameter of pointer is NULL, it is equivalent to malloc

summary:

1. Dynamic memory allocation is a powerful feature in C language

2. Programs have the opportunity to use more memory when needed

3. malloc simply applies for a fixed size of memory from the system

4. calloc can apply for memory in units of type size and initialize it to 0;

5.ralloc is used to reset the memory size


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951054&siteId=291194637