首页 > 其他分享 >Memory Layout of the C program

Memory Layout of the C program

时间:2024-04-26 18:11:49浏览次数:13  
标签:Layout read data memory program Memory initialized segment

reference:

  1. Compilation Steps and Memory Layout of the C Program

  2. Storage Class

  3. RAM明明断电会丢失数据,为什么初始化的全局变量存储在RAM?详细分析程序的存储

Table of Contents

Memory Layout of the C Program

When you run any C program, its executable image is loaded into the RAM of the computer in an organized manner which is called the process address space or memory layout of the C program. This memory layout is organized in the following fashion:

  1. Text or Code Segment
  2. Initialized Data Segments
  3. Uninitialized Data Segments(BSS)
  4. Stack Segment
  5. Heap Segment
  6. Unmapped or Reserved Segments

layout of the c program

Text or Code Segment

The code segment, also known as the text segment which contains the machine code of the compiled program. The text segment of an executable object file is often a read-only segment that prevents a program from accidently modified. So this memory contains .bin or .exe or .hex etc.

As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it.

Data Segments

The data segment stores program data. This data could be in the form of initialized or uninitialized variables, and it could be local or global. The data segment is further divied into four sub-data segments(initialized data segment, uninitialized or .bss data segment, stack and heap) to store variables depending upon if they are local or global, and initialzed or uninitialized.

Initialized Data Segment

Initialized data or simply data segment stores all global, static, constant and external variables(declared with extern keyword) that are initialized beforehand.

Note that, that data segment is not read-only, since the values of the variables can be changed at run time.

This segment can be further classified into the initialized read-only area and the initialized read-write
area.

All global, static and external variables are stored in initialized read-write memory except the const variable.

// This will be stored in initialized read-only memory
const int i = 100;

// This will be stored in initialized read-write memory
int j = 1;
char c[12] = "EmbeTronicX"

int main()
{

}

Uninitialized Data Segment

The uninitialized data segment is also called as BSS segment. BSS stands for Block Started by Symbol named after an ancient assembler operator. The uninitialized data segment contains all global and static variables that are initialzed to zero or do not have explicit initialization in the source code.

// This will be stored in uninitialized read-only memory
static int i = 0;
int j;

int main()
{

}

ATTENTION: I think the

标签:Layout,read,data,memory,program,Memory,initialized,segment
From: https://www.cnblogs.com/archerqvq/p/18160421

相关文章

  • Compilation Steps and Memory Layout of the C program
    TableofContentsTableofContentsWhatarethefourstagesofthecompilationprocess?PreprocessingCompilationAssemblyLinkingWhatarethefourstagesofthecompilationprocess?NormallycompilingaCprogramisamulti-stageprocessandut......
  • 第三章:Memory Consistency Motivation and Sequential Consistency
    chapter3:内存为什么需要consistency和顺序Consistency本章深入研究内存consistency模型,这些模型为程序员和实现者定义了共享内存系统的行为。这些模型定义了行为正确性,以便程序员知道期望什么,实现者知道提供什么。1、共享内存行为存在的问题要了解为什么必须定义共享内存行......
  • 【翻译】RISC-V裸机编程指南(Bare metal programming with RISC-V guide)
    RISC-V裸机编程指南(BaremetalprogrammingwithRISC-Vguide)作者:Follow@popovicu94原文链接:https://popovicu.com/posts/bare-metal-programming-risc-v/今天,我们将探讨如何为RISC-V架构的机器编写一个裸机程序。为了确保可复现,目标平台选择为QEMUriscv64virt虚拟机......
  • Docker(二十)-Docker容器CPU、memory资源限制
    背景在使用docker运行容器时,默认的情况下,docker没有对容器进行硬件资源的限制,当一台主机上运行几百个容器,这些容器虽然互相隔离,但是底层却使用着相同的CPU、内存和磁盘资源。如果不对容器使用的资源进行限制,那么容器之间会互相影响,小的来说会导致容器资源使用不公平;大的来说,可......
  • 2022 China Collegiate Programming Contest (CCPC) Mianyang | 2022 CCPC 绵阳(MAED
    搬运自本人知乎文章。https://zhuanlan.zhihu.com/p/588646549M.Rock-Paper-ScissorsPyramid题目链接Problem-M-Codeforces题意有一个长度为\(n\)的石头剪刀布序列,每个元素是RPS(石头、布、剪刀)中的一个,我们需要用这个序列构造一个三角,三角的底层为这个序列,第\(i(......
  • Cadence Virtuoso 打开 Layout 显示 undefined packet
    软件版本:IC617操作系统:CentOS解决办法:临时方法打开layout后。按住shift,点选左边layers窗口中的任一层次,弹出displayResourceEditor窗口。file->load。选择需要的drf文件加载即可。永久方法把display.drf文件复制到你的virtuoso启动目录即可参考链接......
  • Spark设置executor-memory后,executor显示的内存不符问题
    以该命令为例,我们设置一个executor并分配内存为2800m,可以看到ui上只给executor分配了1.5G的内存。同理,如果你设置了--executor-memory=1g,那么实际上只会有1048.8M的内存会被分配。spark-submit--masteryarn\--num-executors=1\--executor-memory=2800m\--classorg.apac......
  • The 18-th Beihang University Collegiate Programming Contest (BCPC 2023) - Final
    https://codeforces.com/gym/104883A#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;usingvi=vector<int>;i32main(){ios::sync_with_stdio(false),cin.tie(nullptr);i64n,sum=0;c......
  • Bulk端不接到地的NMOS,怎么画Layout?(搬运自LayoutArt)
    在上图中,红圈中的NMOS,它们的Bulk端没有接到地,而是接到各自的Source端,这种情况下,Layout应该怎么画呢?首先,大致了解NMOS的Bulk端为什么不接地,而是接到Source?在有些电路设计中,NMOS的Vth太大,NMOS管不能正常工作,为了减少Vth(阈值电压),可能会使用低Vth的NMOS,在工艺允许时,也可能使用减少体......
  • Kernel panic - not syncing: Out of memory: system-wide panic_on_oom is enabled
    内存不足,导致Java 进程被杀掉。 [1534.300650]Kernelpanic-notsyncing:Outofmemory:system-widepanic_on_oomisenabled[1534.301803]CPU:5PID:2930Comm:javaKdump:loadedTainted:GO5.10.0-60.18.0.50.r1083_58.hce2.x86_64#1[153......