5. Linux Application Address layout

Source: Internet
Author: User

5. Linux Application Address layout

Program Composition:

When you learn Linux application development, you often experience the following concepts: Code snippets, data segments, BSS segments (Block Started by Symbol, AKA: Uninitialized data segments), heap (heap), and stack (stack). These parts are also an important part of the Linux application.

Memory layout:

When a Linux application runs in memory, the above components are laid out in memory:

    1. From low address to high address are: Code snippet, data segment, BSS segment, heap, stack.
    2. The heap grows to a high memory address.
    3. The stack grows to a low memory address.

The following program is tested below:

To view the threads of a system:

Here we look at the address assignment for this code:

Address of data storage:

    1. Code snippet: Code, global constant (const), string constant.
    2. Data segments: Global variables (initialization and uninitialized), static variables (global and local, initialized, and uninitialized).
    3. Heap: Dynamically allocated regions
    4. Stacks: Local variables (initialization and uninitialized, but excluding static variables), local read-only variables (const).

?

?

?

From the above: The starting address of the code snippet is 8048000. Data segment start Address 8049000, heap start address b77a5000, stack starting address bfb47000.

?

?

Analysis Data Storage location:

Example code:

#include <stdio.h>

#include <stdlib.h>

int global_init_a=1; Global init variable

int global_uninit_a; Global UnInit variable

static int static_global_init_a=1;

static int static_global_uninit_a;

const int const_global_a=12;

?

int global_init_b=2; Global init variable

int global_uninit_b; Global UnInit variable

static int static_global_init_b=12;

static int static_global_uninit_b;

const int const_global_b=12;

?

int main ()

{

???? int local_init_a=1;

???? int local_uninit_a;

???? static int static_local_init_a=1;

???? static int static_local_uninit_a;

???? const int const_local_a=12;

?

???? int local_init_b=1;

???? int local_uninit_b;

???? static int static_local_init_b=1;

???? static int static_local_uninit_b;

???? const int const_local_b=12;

???? int * MALLOC_P_A;

???? Malloc_p_a=malloc (sizeof (int)); //

?

?

???? printf ("&global_init_a =%p\n", &global_init_a);

???? printf ("&global_uninit_a =%p\n", &global_uninit_a);

???? printf ("&static_global_init_a =%p\n", &static_global_init_a);

???? printf ("&static_global_uninit_a =%p\n", &static_global_uninit_a);

???? printf ("&const_global_a =%p\n", &const_global_a);

?

???? printf ("&global_init_b =%p\n", &global_init_b);

???? printf ("&global_uninit_b =%p\n", &global_uninit_b);

???? printf ("&static_global_init_b =%p\n", &static_global_init_b);

???? printf ("&static_global_uninit_b =%p\n", &static_global_uninit_b);

???? printf ("&const_global_b =%p\n", &const_global_b);

?

???? printf ("&local_init_a =%p\n", &local_init_a);

???? printf ("&local_uninit_a =%p\n", &local_uninit_a);

???? printf ("&static_local_init_a =%p\n", &static_local_init_a);

???? printf ("&static_local_uninit_a =%p\n", &static_local_uninit_a);

???? printf ("&const_local_a =%p\n", &const_local_a);

?

???? printf ("&local_init_b =%p\n", &local_init_b);

???? printf ("&local_uninit_b =%p\n", &local_uninit_b);

???? printf ("&static_local_init_b =%p\n", &static_local_init_b);

???? printf ("&static_local_uninit_b =%p\n", &static_local_uninit_b);

???? printf ("&const_local_b =%p\n", &const_local_b);

?

???? printf ("&malloc_p_a =%p\n", &malloc_p_a);

?

?

???? return 0;

}

?

?

Analyze BSS Segments:

A BSS segment is a sub-segment of a data segment.

5. Linux Application Address layout

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.