Analysis of Linux memory segments

Source: Internet
Author: User

The memory allocation for Linux applications is distinguished by Segment (segment), which is viewed using the size command:

Size A.out    text       data        BSS        Dec        hex    filename   1639         the          8       1911        777    A. out

Visible, the data is assigned to the text segment, data segment, BSS section, where the use of the size command can only see the size of the three segments (the compilation stage can be determined), and the heap, the stack is running at the time of the dynamic allocation of space.

I. Distribution of LINUX segments

Text: Code snippet, storing read-only data such as code, constants, strings, etc.

Data: A segment that holds the part of a global variable and a static variable that has been initialized. Here initialization, the initialized value must not be zero

The BSS:BSS segment, which holds global variables that are not initialized, static variables, and initialized global variables and static variables with a value of zero.

Heap: Allocation amount for dynamic memory Oh, the program ape manually calls malloc for allocation and deallocation.

Stack: A parameter that stores local variables and functions, and so on.

Two, the size of the comparison

To calculate the size of a size a.out

The original size;

int Main () {        printf ("hellow world\n");}   Text       data        BSS        Dec        hex    filename            1132           8       1396        574    A. out

1. Local variables are stored in the stack:

int Main () {        int  A;        printf ("hellow world\n");} ~    Text       data        BSS        Dec        hex    filename   1132        256           8       1396        574    A. out

2, local variables (local arrays) are also stored in the stack:
int Main () {        int  A;         int b[];        printf ("hellow world\n");}   Text       data        BSS        Dec        hex    filename            1132          8       1396        574    A. out

3. String (constant is stored in text)
int Main () {        int  A;         int b[] = L"qxj511";        printf ("hellow world\n");}   Text       data        BSS        Dec        hex    filename            1639          8       1903        76f    A. out

4. Static variables that are not initialized are stored in the BSS segment:

intMain () {intA; intb[ -] = L"qxj511"; StaticC; printf ("Hellow world\n");} Text data BSS Dec hex filename1639         the          A       1907        773A. out

5. The initialized static variable is stored in the data segment:
intMain () {intA; intb[ -] = L"qxj511"; Staticc =1; printf ("Hellow world\n");} Text data BSS Dec hex filename1639        260          8       1907        773A. out

6. All variables that are not initialized are stored in the BSS terminal:
intD;intMain () {intA; intb[ -] = L"qxj511"; Staticc =1; printf ("Hellow world\n");} Text data BSS Dec hex filename1639        260          A       1911        777A. out

7. The initialized global variables are stored in the data segment:
intD =1;intMain () {intA; intb[ -] = L"qxj511"; Staticc =1; printf ("Hellow world\n");} Text data BSS Dec hex filename1639         the          8       1911        777A. out

Analysis of Linux memory segments

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.