Memory Allocation in the process environment

Source: Internet
Author: User

Iso c describes three functions for dynamic memory space allocation:

(1)Malloc. Allocate a storage area with a specified number of bytes. The initial values in this bucket are uncertain.

(2)Calloc. Allocate storage space for a specified number of objects with a specified length. Every bit in the space is initialized to 0.

(3)Realloc. Change the length (increase or decrease) of the previous distribution area ). When the length is increased, you may need to move the content of the previous distribution area to another area that is large enough to provide the added storage area at the end (at this time, the pointer of the new distribution area is returned, otherwise, the original pointer value is returned, while the initial value in the new region is not sure.

#include <stdlib.h> * * *realloc(  * free(  *ptr );

The pointers returned by these three assignment functions must be properly aligned so that they can be used for any data object.

Because all three alloc functions return the universal pointer void *, if the # include <stdlib. h> (to obtain the function prototype), when we assign the pointers returned by these functions to different types of pointers, we do not need to explicitly execute type forced conversion.

The function free releases the bucket pointed to by ptr. The released space is usually sent to the available storage zone pool. In the future, the space can be distributed when the above three allocation functions are called.

The realloc function allows us to increase or decrease the length of the previously allocated area (the most common usage is to increase the area ). When the partition is added, if there is sufficient space for expansion in the storage area, it can be expanded to the high address direction in the original storage area without moving any original content, and return the same pointer value sent to it. If there is not enough space in the original storage area, realloc allocates another storage area that is large enough to copy the content in the original storage area to the new storage area. Then, release the original storage area and return the pointer of the new distribution area. Because such a bucket may move its location, no pointer should be directed to it.

Note that the last parameter of realloc is the newsize (New length) of the storage area, which is not the difference between the length of the new and old storage areas. As a special case, if ptr is a null pointer, realloc functions the same as malloc and is used to allocate a storage area with a specified length of newsize.

These allocation examples are implemented through Common sbrk (2) system calls. The system calls the heap of extended (or reduced) processes.

Although sbrk can expand or reduce the storage space of processes, most implementations of malloc and free do not reduce the storage space of processes. The released space is available for future redistribution, but is usually kept in the malloc pool without being returned to the kernel.

It should be noted that the storage space allocated by most implementations is slightly larger than the requirement, additional space is used to record management information-the length of the allocated block, pointer to the next allocated block, and so on. This means that if a write operation is performed on the end of more than one allocated area, the management record of the last block will be overwritten. This type of error is catastrophic, but it is difficult to find it because it will not be exposed quickly. Similarly, the Management Record of the block will be overwritten when the write operation is performed before the start position of the allocated area.

Write operations are performed before or after the dynamically allocated buffer zone, which may not only damage the management record information of the zone. The storage area before and after the buffer area is dynamically allocated may be used for other dynamically allocated objects. These objects may not be related to the code that destroys them, which makes it more difficult to seek the source of information destruction.

Other possible fatal errors include releasing a released block and calling the free pointer instead of the return values of the three alloc functions. If a process calls the malloc function but forgets to call the free function, the memory occupied by the process increases continuously. This is called a leak ). If you do not call the free function to release space that is no longer in use, the length of the process address space will gradually increase until there is no free space. In this case, the performance is reduced due to excessive paging overhead.

Because memory allocation errors are difficult to trace, some systems provide another implementation version of these functions. Each time one of the three allocation functions is called or free, they all carry out additional errors. If you specify a dedicated library when calling the connection editor, you can use functions of this version in the program. In addition, there are public available resources. Using a special flag during compilation will make the additional runtime check take effect.

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.