首页 > 后端开发 > C++ > 正文

C 中的内存布局

WBOY
发布: 2024-07-23 17:25:45
原创
982 人浏览过

介绍

内存布局是指计算机内存的组织和结构方式。它定义了各种系统组件如何划分和使用内存。

这在 C 中至关重要,因为它直接影响变量、函数和数据结构在执行期间的存储和访问方式。

在本文中,我们将了解 C 中内存布局的基本方面。

C 内存布局中的段

C中的内存布局由不同的段组成,下面是段;

  1. 文本(代码)段。
  2. 数据段。
  3. 堆。
  4. 堆栈。

下图描述了 C 的内存布局。

Diagram of C’s memory layout.
现在让我们详细讨论这些片段。

文本(代码)段

文本段是 C 程序中存储编译后的机器代码指令的内存区域。这些指令构成了程序的可执行逻辑,并负责定义其行为。

下面是一个简单的例子来说明 C 程序中文本段的概念:

#include <stdio.h>

int main() {
    int x = 5;
    int y = 10;
    int sum;

    sum = x + y;
    printf("The sum of %d and %d is %d\n", x, y, sum);

    return 0;
}
登录后复制

编译器在编译该程序时将源代码转换为机器代码。该机器代码构成了程序的逻辑和行为,并存储在文本段中。

虽然我们无法直接看到机器码。我们可以理解,文本段包含了编译后的指令。

本质上,文本段包含定义程序执行时如何行为的指令。

数据段

数据段分为两部分:

  • 初始化数据段
  • 未初始化的数据段

初始化数据段

初始化数据段由预先初始化的全局变量、外部变量、静态变量(局部变量和全局变量)和常量全局变量组成。初始化的数据段有两个部分,只读读写部分。

具有可以修改的预定义值的变量,即初始化的全局变量、外部变量和静态(本地和全局)变量存储在读写部分中。另一方面,常量变量位于 只读 部分。

这是一个示例,说明了存储在初始化数据段中的变量,包括读写部分和只读部分:

#include <stdio.h>

// Global variable (read-write section)
int globalVar = 10;

// External variable declaration (read-write section)
extern int externVar;

// Static global variable (read-write section)
static int staticGlobalVar = 20;

// Constant global variable (read-only section)
const int constGlobalVar = 30;

int main() {
    globalVar += 5;
    staticGlobalVar += 10;

    printf("Global variable: %d\n", globalVar);
    printf("Extern variable: %d\n", externVar);  // Assuming externVar is defined in another file
    printf("Static global variable: %d\n", staticGlobalVar);
    printf("Constant global variable: %d\n", constGlobalVar);

    return 0;
}

登录后复制

这说明了存储在初始化数据段的读写和只读部分中的变量。

未初始化的数据段

未初始化的数据段也称为BSS(由符号开始的块)段由未初始化的全局变量、外部变量和静态(本地和全局)变量组成。

这些变量在程序执行之前默认初始化为零。他们具有读写权限。从而允许在程序执行期间读取和写入它们。

示例:

#include <stdio.h>

// Uninitialized global variable (goes to the BSS segment)
int globalVar;

// Uninitialized static global variable (also goes to the BSS segment)
static int staticGlobalVar;

int main() {
    // Uninitialized local static variable (goes to the BSS segment)
    static int staticLocalVar;

    printf("Uninitialized Global Variable: %d\n", globalVar);
    printf("Uninitialized Static Global Variable: %d\n", staticGlobalVar);
    printf("Uninitialized Static Local Variable: %d\n", staticLocalVar);
    return 0;
}

登录后复制

在此程序中,未初始化的变量默认包含零或空值。这是由于编译器自动初始化造成的。这显示了存储在 BSS 段中的变量的行为。

堆是运行时用于动态内存分配的内存区域。这允许在程序执行期间根据需要分配和释放内存。 malloc()calloc()realloc()free() 等函数用于内存分配和释放在堆中。程序的所有部分都可以访问堆。

示例:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // Dynamically allocate memory for an integer variable on the heap
    int *ptr = (int *)malloc(sizeof(int));

    return 0;
    }
登录后复制

此代码片段演示了 C 语言中动态内存分配的简单用法。它引起人们对请求内存、初始化指向该内存的指针以及正确管理内存以避免泄漏所涉及的步骤的关注。虽然此示例中不包括错误处理和内存释放,但这些是实际应用中使用动态内存的关键组成部分。

栈段主要功能是管理函数调用和存储局部变量。这部分在程序的内存布局中至关重要,因为它控制程序内的流程。堆栈采用后进先出(LIFO)结构,这意味着最近添加的数据将首先被删除。这使得堆栈对于管理局部变量和嵌套函数调用非常有效。

示例:

#include <stdio.h>

void functionA(int n) {
    int a = n + 1; // Local variable
    printf("In functionA, a = %d\n", a);
}

void functionB() {
    int b = 10; // Local variable
    printf("In functionB, b = %d\n", b);
    functionA(b); // Call to functionA
}

int main() {
    int x = 20; // Local variable
    printf("In main, x = %d\n", x);
    functionB(); // Call to functionB
    return 0;
}
登录后复制

The code explains how stack frames store local variables. New stack frames are created by the function calls and are eliminated when the functions return. The printf instructions facilitate the visualization of each function's local variable values. The execution flow follows the calls to and returns from functions.

Conclusion

C programmers can improve their coding techniques and gain a better understanding of how their programs interact with memory by mastering these concepts. Understanding memory layout is a vital skill in your programming toolbox, whether you're optimizing for performance or troubleshooting a complex problem.

Feel free to follow, comment, and leave claps. Happy Coding!

Let’s connect on LinkedIn.

以上是C 中的内存布局的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!