Dynamic Allocation and release of C language memory

Source: Internet
Author: User
I. Memory Allocation

The memory allocation mainly uses the marking method.

There are two allocation methods: static allocation, that is, the memory allocation is completed during program compilation. For example, when an array is declared and the length is specified, it will be allocated when it requires compilation.

Another method is dynamic allocation, which means that the program allocates memory for it at runtime.

During memory allocation, the operating system will mark the allocated memory. The operating system will also mark the unallocated memory, the operating system determines whether the memory can be used by marking. When the memory is released, the marking will be modified.

Ii. Dynamic Allocation

Function C provides four functions for dynamic memory allocation and release.Malloc, calloc, realloc, and free. Among them, the first three are the execution of dynamic memory allocation, and the last is the execution of release.

1. Void * malloc (size_t size)

The parameter required by the malloc function is the length of the byte to be allocated, for example

[CPP]View
Plaincopy

  1. Int * P;
  2. P = (int *) malloc (100 );

For the above Code, if an integer occupies four bytes, 25 Integers of memory will be allocated. To increase code portability, if you want to allocate 25 Integers of memory, you should change the code

[CPP]View
Plaincopy

  1. Int * P;
  2. P = (int *) malloc (25 * sizeof (INT ));

When the malloc function allocates memory, it extracts a suitable memory from the memory and returns the first address of the memory. This memory is not initialized. This method will inevitably cause problems, that is, the size of the available memory cannot meet the memory required by the request. At this time, the malloc function returns NULL. Therefore, after the malloc function is used to allocate memory, the memory allocation cannot be guaranteed. When using the malloc function, you should first determine whether the pointer is null. If it is not null, the allocation is successful, yes. Otherwise, vice versa.

2. Void * calloc (size_t num_elements, size_t element_size)

The parameters required by the calloc function are the number of elements and the length of the elements in bytes. These two parameters are required because the calloc function initializes the memory when allocating memory, if Initialization is required, you need to know the size of an element. to calculate the total memory size, you should also know the number of elements.

The main difference between the calloc function and the malloc function is that the initialization is performed before the pointer to the first address is returned. If the program only wants to store some values in the array, this Initialization is a waste.

3. Void * realloc (void * PTR, size_t new_size)

The parameters required by the relloc function are the length of the new memory at the first address of the original memory.

The realloc function is used to modify the size of allocated memory. This function can be used to increase or decrease the memory size. If a piece of memory is expanded, the original content of the memory will be retained, and the newly added memory will be added to the back of the original memory. If it is reduced, the memory at the end will be released.

There is still a problem of insufficient memory for memory expansion. If the allocation fails, null is returned. In addition, another problem is that when the memory is expanded, the size of the original memory cannot be modified. At this time, the system will re-allocate a memory and copy the original memory data to the new memory, therefore, after you use the realloc function to modify the memory size, you should use the pointer returned by the realloc function instead of the original pointer.

4. void free (void * pointer)

The parameter required by the free function is a pointer. The function is to release the memory area pointed to by the pointer (implemented by modifying the mark) so that the memory can be reused.

The free function only releases the memory to which the Pointer Points. the space occupied by the pointer is not changed. After the free function is called, the address to which the Pointer Points remains unchanged. If it is behind the program, re-allocate the memory to which the Pointer Points. The current pointer can also be accessed, but it is not safe. In this case, the pointer is called a wild pointer. To avoid a wild pointer, set the value of the pointer to null after the free function is called.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.