Data zone, code zone, heap zone, stack Zone

Source: Internet
Author: User

Generally, global variables are stored in the data zone, and local variables are stored in the stack zone,
Dynamic variables are stored in the heap area, and function code is stored in the code area.
---------------------------------------------------------------

The stack area is a common stack data structure. It complies with the LIFO post-first-in-first-out rules and local variables are arranged there as defined in ASM, so that the stack can be balanced after a function is completed, simple operation and High Efficiency
The heap (Dynamic zone) here should be called a stack (do not mix with the heap in the data structure), which is a block generated by the program during compilation to generate dynamic memory allocation, stack operations are much more troublesome, the optimal address should be determined during allocation (to prevent useless memory fragments from being generated (due to repeated new and delete requests, small spare memory in the memory of the two blocks being used (not easily allocated) )), the allocation and recycling efficiency is much lower than the stack efficiency.
---------------------------------------------------------------

Stack is a function provided by the system. It features fast and efficient. Its disadvantage is that it has restrictions and data is not flexible. Stack is a function provided by the function library, which features flexibility and convenience and wide data adaptability, but the efficiency is reduced. The stack is the system data structure, which is unique for processes/Threads. The heap is the internal data structure of the function library, which is not necessarily unique. Memory allocated by different heaps cannot be operated on each other. Stack space is divided into static allocation and dynamic allocation. Static allocation is completed by the compiler, such as automatic variable allocation. Dynamic Allocation is completed by the alloca function. The stack does not need to be released dynamically (automatically), so there is no release function. For the sake of porting> Programs, dynamic stack allocation is not encouraged! Heap space allocation is always dynamic. Although all data spaces are released back to the system at the end of the program, precise memory allocation and memory allocation are the basic elements of a good program.

 

Here is an example to illustrate the differences between these "segments ".

 

# Include <malloc. h>

Unsigned char gvch;

Unsigned char short gvshort;

Unsigned int gvint = 0x12345645;

Unsigned long gvlong = 0x87654321;

Void main ()

{

Unsigned char array [10], * P;

P = malloc (10 * sizeof (char ));

While (1 );

}

-----------------------------------------------------------------

The code in the above program is stored in the code area, such as while (1 );

The global variables gvch and gvshort are stored in the BSS segment because they do not have initial values.

The gvint and gvlong global variables have the initial values, so they are stored in the data segment.

The array and P variables are local variables, so they are stored in the stack zone.

The space applied by the malloc function is dynamic, so it is stored in the: heap area.

 

Bytes -------------------------------------------------------------------------------------------------------

Supplement:

BSS (block started by symbol) is usually a memory area used to store uninitialized global variables and static variables in the program.

Note the difference from the data segment. BSS stores uninitialized global and static variables, and the data segment stores initialized global and static variables.

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.