C Language program compiled memory allocation

Source: Internet
Author: User

C-Language program compiled memory allocations:

1. Stack area (stack)-the compiler automatically assigns the release, the main storage function parameter value, local variable value, etc.

2. Heap area--distributed by the programmer to release;

3. Global or static zone--store global variables and static variables; released by the system at the end of the program, divided into global initialization zone and global uninitialized zone;

4. Character constant-The constant string is placed with this, the program is released at the end of the system;

5. Program code area-the binary code that holds the function body

Example://MAIN.C

int a=0; Global initialization Zone

Char *p1; Global uninitialized Zone

void Main ()

{

int b; Stack

char s[]= "BB"; s in the Stack, "BB" in the constant area

Char *p2; Stack

Char *p3= "123"; Where, "123/0" constant area, p3 in the stack area

static int c=0; Global Zone

p1= (char*) malloc (10); 10-byte area in the heap area

strcpy (P1, "123"); "123/0" in the constant area, the compiler may be optimized to point to the same area as the P3

The memory that a C program occupies can be divided into the following categories:

(a) stack

This is an area that is automatically allocated and freed by the compiler. Parameters of main storage functions, local variables of functions, etc. When a function begins execution, the local variable is pushed into the stack, and the arguments and variables that were previously entered into the stack are released as soon as the function is executed. It works like a stack in a data structure.

(ii) heap

This is the area that the programmer controls to allocate and release, and in C, the space allocated by the malloc () function exists on the heap. The space allocated on the heap is not automatically released as the stack does after a function has been executed, but it persists throughout the program's run time. Of course, if you do not manually release the space (the free () function), the system will automatically release it after the program has finished running. You may not feel the impact on a small program, but for large programs, such as a large game, you will encounter a problem with insufficient memory.

(iii) Global area

The global variables and static variables in c are stored in the global zone. They are a bit like the space on the heap, and they persist throughout the entire run of the program, but the difference is that they are assigned and freed by the compiler's own control.

(iv) Literal constant area

For example char *c = "123456"; then "123456" is a literal constant, stored in the literal constant area. Distribution and deallocation are also controlled by the compiler.

(v) Program code area

Binary code that holds the body of the function.

2. Example (i)

int a = 0; Global Zone

void Main ()

{

int b; Stack

Char s[] = "ABC"; s in the Stack, "ABC" in the literal constant area

Char *p1,*p2; Stack

Char *p3 = "123456"; "123456" in the constant area, p3 on the stack

static int c = 0; Global Zone

P1 = (char *) malloc (10); P1 on Stack, 10 bytes allocated in heap

P2 = (char *) malloc (20); P2 on Stack, 20 bytes allocated in heap

strcpy (P1, "123456"); "123456" is placed in the constant area

The compiler might optimize it to a place with the "123456" that P3 points to.

}

3. Example (ii)

Returns a char-type pointer

Char *f ()

{

The s array is stored on the stack

Char s[4] = {' 1 ', ' 2 ', ' 3 ', ' 0 '};

return s; Returns the address of an S array, but the function is freed after the S array is run.

}

void Main ()

{

Char *s;

s = f ();

printf ("%s", s); Print out garbled. Because S is pointing to an address that has no data

}

There is a series of reserved field and pass parameters on the stack when the function is called.

The size of the stack is limited, the VC default is 2M. Stack is not enough to use the situation is generally allocated in the program a large number of arrays and recursive function hierarchy too deep. It is important to know that when a function call is finished, it frees all the stack space in the function. Stacks are automatically managed by the compiler, and you don't have to worry about them.

The heap is dynamically allocated memory, and you can allocate it with very large memory. However, a memory leak is caused by bad use. and frequently malloc and free generate memory fragmentation (a bit like disk fragmentation), because C allocates dynamic memory when it is looking for matching memory. The stack does not produce fragmentation, and accessing data on the stack is faster than accessing the data on the heap via pointers. Generally speaking, stacks and stacks are the same, that is stack, and heap is the heap. Stacks are first-in, usually from high-address to low-address growth.

Heap and Stack (stack) are the two basic concepts that are inevitably encountered in C/s + + programming. First of all, these two concepts can be found in the Book of data structure, they are the basic structure, although the stack is simpler. These two concepts are not parallel in a specific C + + programming framework. The study of the underlying machine code reveals that the stack is the data structure provided by the machine system, while the heap is provided by the C/S function library. Specifically, the modern computer (serial execution mechanism) directly supports the data structure of the stack at the bottom of the code. This is reflected in, there is a dedicated register to the address of the stack, there is a dedicated machine instruction to complete the data into the stack out of the stack operation. The mechanism is characterized by high efficiency, limited data support, generally integer, pointer, floating point and other systems directly supported by the data type, does not directly support other data structures. Because of this feature of the stack, the use of stacks is very frequent in the program. The call to the child program is done directly using the stack. The call instruction of the machine implies that the return address is pushed into the stack and then jumps to the subroutine address, while the RET instruction in the subroutine implicitly pops the return address from the stack and jumps to the operation. The automatic variable in C + + is an example of a direct stack, which is why the automatic variable of the function automatically fails when the function returns.

Unlike stacks, the data structure of a heap is not supported by the system (whether it is a machine system or an operating system), but is provided by a library of functions. The basic Malloc/realloc/free function maintains a set of internal heap data structures. When the program uses these functions to obtain new memory space, the function first tries to find the available memory space from the internal heap, if there is no memory space available, then attempts to use the system to dynamically increase the size of the program data segment memory, the newly allocated space is first organized into the internal heap, It is then returned to the caller in the appropriate form. When the program frees the allocated memory space, this memory space is returned to the internal heap structure and may be appropriately processed (such as merging additional free space into a larger free space) to better fit the next memory allocation request. This complex allocation mechanism is actually equivalent to a memory allocation buffer pool (cache), which has several reasons for using this set of mechanisms:

1. System calls may not support memory allocations of any size. Some system calls only support a fixed size and multiple memory requests (per page allocation), which can be wasteful for a large number of small memory classifications.

2. System calls to request memory can be costly. System calls may involve conversion of the user state and kernel mentality.

3. Memory allocations that are not managed can easily cause memory fragmentation under the allocation release operation of a large amount of complex memory

Comparison of heaps and stacks

From the above knowledge, the stack is the function provided by the system, the characteristics are fast and efficient, the disadvantage is limited, the data is not flexible, and the heap is the function library provides the function, the characteristic is flexible and convenient, the data adapts widely, but the efficiency has a certain reduction. A stack is a system data structure that is unique to a process/thread, and a heap is a library internal data structure that is not necessarily unique. Memory allocated by different heaps cannot operate with each other. The stack space is divided into two kinds: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the automatic variable (auto) assignment. Dynamic allocation is done by the Alloca function. The dynamic allocation of the stack does not have to be released (it is automatic), and there is no release function. For portable programs, the dynamic allocation of stacks is not encouraged! The allocation of heap space is always dynamic, although all data space will be released back to the system at the end of the program, but an accurate memory/release memory match is an essential element of a good program.

C Language program compiled memory allocation

Related Article

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.