C language memory allocation

Source: Internet
Author: User

C language and Memory Allocation Method

(1) distribution from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.

(2) create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.

(3) allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but it has the most problems.

Functions related to memory application in C language include alloca, calloc, malloc, free, realloc, and sbrk.

Alloca applies for memory from the stack, so it does not need to be released. the memory allocated by malloc is located in the heap and does not initialize the memory content. Therefore, basically, after malloc, call the memset function to initialize the memory space.

Calloc will initialize this part of memory, set to 0. while realloc adjusts the memory size applied for by malloc. the Applied memory needs to be released using the function free. sbrk increases the data segment size;

Malloc, calloc, and free are basically implemented by the C function library and have nothing to do with OS. the C function library uses a certain structure to store the current amount of available memory. if the malloc size of the program exceeds the reserved space in the library, the BRK system call will be called to increase the available space and then the space will be allocated. when free, the released memory is not immediately returned to the OS, but stored in the internal structure. for example, BRK is similar to a wholesale database, where a large memory size can be applied to the operating system at one time, while functions such as malloc are similar to retail, meeting the runtime requirements. this mechanism is similar to buffering.

The reason for using this mechanism: system calls cannot support memory allocation of any size (some system calls only support applying for memory of a fixed size and its multiples. In this case, the allocation of small memory will result in a waste; the system call request memory is expensive, involving the conversion of user and core states. both the functions malloc () and calloc () can be used to allocate dynamic memory space, but the two are slightly different.

The malloc () function has a parameter, that is, the size of the memory space to be allocated:

Void * malloc (size_t size );

The calloc () function has two parameters: the number of elements and the size of each element. The product of these two parameters is the size of the memory space to be allocated:

Void * calloc (size_t numelements, size_t sizeofelement );

If the call is successful, both the malloc () and calloc () functions return the first address of the allocated memory space.

The main difference between the malloc () and calloc () functions is that the former Cannot initialize the allocated memory space, while the latter can. If the memory space allocated by the malloc () function has never been used before, each of them may be 0; otherwise, if the memory space has been allocated, released, and re-allocated, a variety of data may be left over. That is to say, when a program using the malloc () function is started (the memory space has not been re-allocated), it can run normally, but after a period of time (the memory space has been re-allocated) problems may occur.

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 zero. If you allocate memory to elements of the pointer type, these elements are usually (but cannot be guaranteed) initialized to a null pointer; if you allocate memory for elements of the real number type, these elements (only in some computers) may be initialized to zero of the floating point type.

Another difference between the malloc () and calloc () functions is that the calloc () function returns an array composed of some objects, but the malloc () function returns only one object. To explicitly allocate memory space for an array, some programmers use the calloc () function. However, apart from whether to initialize the allocated memory space, most programmers consider the following two function call methods:

 

Calloc (numelements, sizeofelement );
Malloc (numelements * sizeofelement );

It should be explained that, theoretically (according to the ansic standard) arithmetic operations of pointers can only be performed in a specified array, but in practice, even if the C compiler or translator follows this rule, many C Programs still break through this restriction. Therefore, although the malloc () function does not return an array, the memory space it allocates can still be used by an array (the same for the realloc () function, even though it cannot return an array ).

In short, when you select between the calloc () function and the malloc () function, you only need to consider whether to initialize the allocated memory space, instead of considering whether the function can return an array.

When the program runs malloc but is not free, memory leakage may occur. some of the memory is not used, but it is not free. Therefore, the system considers that this part of memory is still in use, resulting in a constant request for memory from the system. however, memory leakage only means that when the program is running and the program exits, the OS will reclaim all resources. therefore, restarting the program properly may be helpful.

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.