Text segment, data segment, and BSS segment

Source: Internet
Author: User

Different compiler may have slightly different allocations for storage during compilation, but the basic structure is roughly the same.

Broadly, it can be divided into three segments: text, data, and BSS.

The text section is used for storing code, which is typically mapped as read-only in memory, but data and BSS are writable.

Data storage is usually divided into the following sections:

1, stack: Automatically allocated by the compiler, save the function of local variables and parameters.

2, Heap: Generally by the programmer dynamically allocated release, if the programmer does not release, the program ends may be collected by the OS, such as malloc. It differs from the heap in the data structure, and it is more like a linked list.

3. Global: The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area.

4, text constant area: This area in many cases in the code area, because the code snippet and constant are read-only.

5, program code area: the binary code that holds the function body.

6. BSS segment: A global variable and a static variable defined without an initial value, placed in this area, usually just record the variable name and size, equivalent to a placeholder.

Refer to someone else's sample code:

1#include <stdio.h>2#include <stdlib.h>3#include <iostream>4#include <string.h>5 using namespacestd;6 7 Static intA=1;//Global Initialization Zone8 intb=2;//Global Initialization Zone9 Char*p;//Global Uninitialized ZoneTen Char*P2;//Global uninitialized Zone, BSS segment One int*P3;//Global uninitialized Zone, BSS segment A int*P4;//Global uninitialized Zone, BSS segment - Char*p5={"555555555"};//Global Initialization Zone -  the intMain () { -     Static intC=3; -     intD=4;//Memory Stacks -     intE=7;//Memory Stacks +      -     Char*p6={"555555555"}; +P= (Char*)malloc(sizeof(Char)*Ten);//Memory Heap AP2= (Char*)malloc(sizeof(Char)*Ten);//Memory Heap atP3= (int*)malloc(sizeof(int));//Memory Heap -P4= (int*)malloc(sizeof(int)*Ten);//Memory Heap -      for(intI=0; i<=9; i++) p4[i]=0x1; -  -  -*p3=0x123; instrcpy (P,"123456789");//literal constant Area -strcpy (P2,"987654321"); tostrcpy (P2,"123456789"); +}
View Code

Text segment, data segment, and BSS segment

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.