Analysis of code segments, data segments, stack segments, and data segments

Source: Internet
Author: User

1. High address: Stack (stores local variables, function parameters, and other data), grows down (readable and writable and executable)

2. Heap (dynamically allocated memory is used), and grow up (readable, writable, and executable)

3. Data Segment (save global data and static data) (readable and writable cannot be executed)

4. Status address: code segment (save Code) (readable and executable and not writable)

 

 

Code segments are stored in program text, so sometimes called text segments. commands in the instruction pointer are obtained from here. This segment can be shared. For example, if you have two VI in Linux to edit the text, the two VI share a code segment, but the data segment is different (this is a bit similar to sharing the same member function for different objects in C ++ classes ).

Data segments are used to store data. They can also be divided into three areas: the data zone initialized as non-zero, the BSS, and heap. The initialized non-zero data area generally stores static non-zero data and global non-zero data. BSS is the abbreviation of block started by symbol. It is originally a term in assembly language. This area stores uninitialized global data and static data. There is also a heap. This region is used for dynamic memory allocation, that is, the memory allocated by using functions such as malloc is in this region. Its address increases upwards.

The last stack segment (note that the stack is a stack, the heap is a stack, Not The Same Thing). The stack is too important. data such as local variables and function parameters are stored here. For example, recursive algorithms are implemented by stacks. Stack addresses increase downward. The details are as follows:

========= High address ======
Program stack segment

Downward Growth


"Empty" ======

Upward growth

Heap
------ Data Segment
BSS
------
Non-zero data
========= Low address ======

======================
Code snippet
======================

Note that the code segments and data segments are clearly separated, but the data segments and stack segments do not exist. In addition, the stack increases downward and the stack increases upwards, therefore, in theory, the heap and stack will "grow together", but the operating system will prevent such errors, so don't worry too much.

From http://www.cnblogs.com/qlee/archive/2011/09/07/2169831.html

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.