BSS,堆栈,堆,数据,代码/文本-这些元素分别在内存中的何处开始? [英] BSS, Stack, Heap, Data, Code/Text - Where each of these start in memory?

查看:83
本文介绍了BSS,堆栈,堆,数据,代码/文本-这些元素分别在内存中的何处开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内存段-BSS,堆栈,堆,数据,代码/文本(还有吗?).

Segments of memory - BSS, Stack, Heap, Data, Code/Text (Are there any more?).

说我有128MB RAM,有人可以告诉我:

Say I have a 128MB RAM, Can someone tell me:

  • 为这些内存段中的每一个分配多少内存?

  • How much memory is allocated for each of these memory segments?

他们从哪里开始?请指定地址范围或类似的内容,以使内容更清晰.

Where do they start? Please specify the address range or something like that for better clarity.

哪些因素影响应该从哪里开始?

What factors influence which should start where?

推荐答案

该问题取决于所用变量的数量.由于您未指定哪种编译器,语言或什至操作系统,因此很难确定!这完全取决于负责应用程序的内存管理的操作系统.简而言之,对于这个问题没有明确的答案,请想一想,运行时的编译器/链接器要求操作系统分配一块内存,该分配取决于有多少个变量,它们有多大,变量的范围和用法.例如,这个简单的C程序在一个名为simpletest.c:

That question depends on the number of variables used. Since you did not specify what compiler or language or even operating system, that is a difficult one to pin down on! It all rests with the operating system who is responsible for the memory management of the applications. In short, there is no definite answer to this question, think about this, the compiler/linker at runtime, requests the operating system to allocate a block of memory, that allocation is dependent on how many variables there are, how big are they, the scope and usage of the variables. For instance, this simple C program, in a file called simpletest.c:


#include <stdio.h>
int main(int argc, char **argv){
   int num = 42;
   printf("The number is %d!\n", num);
   return 0;
}

假设环境是基于Unix/Linux的,并且是这样编译的:

Supposing the environment was Unix/Linux based and was compiled like this:


gcc -o simpletest simpletest.c

如果要在二进制映像simpletest上发布objdumpnm,您将看到可执行文件的各个部分,在这种情况下为'bss','text'.记下这些部分的大小,现在在上面的代码中添加int var[100];,重新编译并重新发出objdumpnm,您会发现data部分已出现-为什么?因为我们添加了一个int数组类型的变量,该变量具有100个元素.

If you were to issue a objdump or nm on the binary image simpletest, you will see the sections of the executable, in this instance, 'bss', 'text'. Make note of the sizes of these sections, now add a int var[100]; to the above code, recompile and reissue the objdump or nm, you will find that the data section has appeared - why? because we added a variable of an array type of int, with 100 elements.

此简单练习将证明这些部分会增加,因此二进制文件会变大,并且还将证明您无法预先确定要分配多少内存,因为运行时实现因编译器和编译器以及操作系统的不同而不同到操作系统.

This simple exercise will prove that the sections grows, and hence the binary gets bigger, and it will also prove that you cannot pre-determine how much memory will be allocated as the runtime implementation varies from compiler to compiler and from operating system to operating system.

简而言之,操作系统会在内存管理上大放异彩!

In short, the OS calls the shot on the memory management!

这篇关于BSS,堆栈,堆,数据,代码/文本-这些元素分别在内存中的何处开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆