"C Language Practice every day (24)" Memory allocation

Source: Internet
Author: User

Introduction:

For a C language program, it is useful to understand how it is allocated in memory when it executes and how it is implemented. Here is a summary of some of the C language Program memory allocation knowledge.


One

A C program. The running files formed after compiling the connection are usually composed of sections of code, data segments, heaps and stacks. The data segment contains only read data segments, initialized read and write data segments, and uninitialized BSS segments. For example, as seen in:


Text snippet: The code that holds the program running.


Data segment:

1> Read Only the data segment:

Read-only data segments are data that the program uses that are not changed, and use this data in a way that looks like a table-type operation, because these variables do not need to be changed, so they simply need to be placed in read-only memory.

Variables that are typically const-modified and literal constants used in programs are usually stored in a read-only data segment.

2> initialized read-write data segment:

Initialized data is a variable that is declared in a program and has an initial value. These variables need to occupy memory space, and they need to be in a writable memory area when the program executes. And there is an initial value to read and write when the program executes. In a program, it is generally a global variable that has already been initialized, a static local variable that has already been initialized (the initialized variable of the static modifier)

3> uninitialized Segment (BSS):

Uninitialized data is declared in the program, but there are no initialized variables that do not need to occupy memory space before the program executes.

Similar to the read-write data segment, it also belongs to the static data area.

However, the data in this paragraph has not been initialized.

Uninitialized data segments are only generated during the initialization phase of execution, so its size does not affect the size of the target file. In a program, there are generally no initialized global variables and static local variables that are not initialized.


Heap: requires the program ape to apply itself (call Malloc,realloc,calloc) and indicate the size and release by the program Ape.


Stack: The system itself is assigned voluntarily. For example, declare a local variable int b in the function, and the system itself proactively opens up space for B in the stack.


Two

Based on the above theoretical knowledge, analyze the memory allocation of the demo sample fragment:



Three

Stack-to-heap differences:

1. How to Apply

(1) stack (SATCK): By the system's own initiative to allocate. For example, declare a local variable int b in the function, and the system itself proactively opens up space for B in the stack.

(2) Heap: Requires the program ape to apply for itself (call Malloc,realloc,calloc), and specify the size and release by the program Ape.

Easy to produce memory leak.

Eg:char p;

p = (char *) malloc (sizeof (char));

However,P itself is in the stack.

2. Restrictions on size of applications

(1) Stack: The stack is a data structure that extends to the bottom address, which is a contiguous area of memory (it grows in the opposite direction of memory growth). The size of the stack is fixed. If the requested space exceeds the remaining space on the stack, overflow will be prompted.

(2) Heap: A heap is a data structure with a high address extension (its direction of growth is the same as the direction of memory growth). is a non-contiguous area of memory. This is because the system uses a linked list to store spare memory addresses. Nature is discontinuous, and the traversing direction of the list is from the bottom address to the high address.

The size of the heap is limited by the valid virtual memory in the computer system.

3. System response:

(1) Stack: Only the space of the stack is larger than the application space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow.

(2) Heap: The first thing you should know is that the operating system has a list of spare memory addresses, but when the system receives the application, it iterates through the list. Find the heap node where the first space is larger than the requested space. The node is then removed from the spare linked list. The space of the node is allocated to the program, and for most systems, the size of this allocation is recorded at the first address in the memory space. Such The free statement in the code frees up the memory space correctly. Other than that. The size of the found heap node is not necessarily exactly equal to the size of the application, the system will voluntarily put the extra part of the spare linked list again.

Note: For the heap, frequent malloc/free is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack. There is no such problem,

4. Application efficiency

(1) The stack is allocated by the system itself, and the speed is fast. But the program ape is uncontrollable.

(2) Heap is the memory allocated by malloc, which is generally slower and easier to fragment. It is the most convenient to use.

5. Storage content in heaps and stacks

(1) Stack: The address of the next statement in the main function of the first stack when a function is called. Then there are the arguments for the function, which is the stack from right to left, and then the local variables in the function. Note: Static variables are not in the stack.

When the function call ends. Local variables first out of the stack. Then there is the number of references, and the last stack-top pointer points to the most open address, the next instruction in the main function, and the program continues to run from that point.

(2) Heap: Usually the size of the heap is stored in the heap with one byte in the head.

6. Access efficiency

(1) Heap: char *s1= "Hellow Tigerjibo";

(2) Stack: char s1[]= "Hellow Tigerjibo"; it is assigned at execution time, and the pointer is faster with the pointer than with pointers, and the pointers in the underlying assembly need to be relayed in the edx register, and the array is read on the stack.

Add:

The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, and the stack stack has special instruction to run. This determines the efficiency of the stack is relatively high.

The heap is provided by the C + + function library, and its mechanism is very complex, for example, to allocate a piece of memory. The library function searches the heap memory for a sufficient amount of space available, assuming there is not enough space (possibly due to too much memory fragmentation), according to a certain algorithm (the detailed algorithm can refer to the data structure/operating system). It is possible to invoke system functions to add memory space to the program data segment. This gives you a chance to get enough memory and then return. Obviously. The heap is much less efficient than the stack.

7. Allocation Method:

(1) The heap is dynamically allocated and there is no statically allocated heap.

(2) There are two ways to allocate stacks: static allocation and dynamic allocation. Static allocations are completed by the compiler. For example, the allocation of local variables.

Dynamic allocations are allocated by the ALLOCA function, but the dynamic allocation of stacks is different from the heap.

Its dynamic allocation is released by the compiler without the need for manual implementation.


Four

References 1:http://blog.csdn.net/tigerjibo/article/details/7423728

References 2:http://blog.csdn.net/to_be_it_1/article/details/31420549

References 3:http://blog.csdn.net/lovecodeless/article/details/24384093

References 4:http://blog.csdn.net/lovecodeless/article/details/21084513


"C Language Practice every day (24)" 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.