C/C ++程序的内存布局如何? [英] How is the memory layout of a C/C++ program?

查看:48
本文介绍了C/C ++程序的内存布局如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道其中有一些部分,例如堆栈,堆,代码和数据.堆栈/堆是否使用相同的内存区域,因为它们可以独立增长?这是什么代码段?当我有一个函数时,它是堆栈或代码段的一部分吗?还有这个初始化/未初始化的数据段是什么?

I know that there are sections like Stack, Heap, Code and Data. Stack/Heap do they use the same section of memory as they can grow independently? What is this code section? When I have a function is it a part of the stack or the code section? Also what is this initialized/uninitialized data segment?

是否有只读存储区可用?当我有一个const变量时,实际上发生的是编译器将一个内存部分标记为只读,或者将其放入只读内存部分.

Are there read only memory section available? When I have a const variable, what is actually happening is it that the compiler marks a memory section as read only or does it put into a read only memory section.

静态数据保存在哪里?全球数据保存在哪里?

Where are static data kept? Where are global data kept?

有什么好的参考文献/文章吗?

Any good references/articles for the same?

我认为内存部分和布局与操作系统无关,并且与编译器有更多关系.堆栈,堆,代码,数据[Initialized,Uninitialized]段不是在所有OS中都出现吗?当存在静态数据时,编译器将其理解为静态,这是怎么回事,下一步该怎么做?是由编译器管理程序,它应该知道该怎么做?所有编译器都不应该遵循共同的标准吗?

I thought the memory sections and layout are OS independent and it has more to do with compiler. Doesn't Stack, Heap, Code, Data [Initialized, Uninitialized] segment occur in all the OS? When there is a static data, what is happening the compiler has understood it is static, what next, what will it do? It is the compiler which is managing the program and it should know what to do right? All compilers shouldn't they follow common standards?

推荐答案

关于C ++内存布局的定义实际上很少.但是,大多数现代OS使用的系统都有些相似,并且各段是根据权限分开的.

There's very little that's actually definitive about C++ memory layouts. However, most modern OS's use a somewhat similar system, and the segments are separated based on permissions.

代码具有执行权限.其他细分市场则没有.在Windows应用程序中,您不能仅将一些本机代码放在堆栈上并执行.Linux提供了相同的功能-它处于x86架构中.

Code has execute permission. The other segments don't. In a Windows application, you can't just put some native code on the stack and execute. Linux offers the same functionality- it's in the x86 architecture.

数据是作为结果一部分(.exe等)但不能写入的数据.这部分基本上是文字的去向.本部分仅具有读取权限.

Data is data that's part of the result (.exe, etc) but can't be written to. This section is basically where literals go. Only read permission in this section.

这两个段是结果文件的一部分.堆栈和堆是在运行时分配的,而不是映射到硬盘驱动器之外.

Those two segments are part of the resulting file. Stack and Heap are runtime allocated, instead of mapped off the hard drive.

堆栈本质上是一个大的堆分配(大约1MB,许多编译器为此提供了设置).编译器为您管理.

Stack is essentially one, large (1MB or so, many compilers offer a setting for it) heap allocation. The compiler manages it for you.

堆内存是操作系统通过某些过程返回给您的内存.通常,堆是指向空闲内存块及其大小的指针的堆(数据结构).当您请求一个时,它就会交给您.都具有读取和写入权限,但没有执行权限.

Heap memory is memory that the OS returns to you through some process. Normally, heap is a heap (the data structure) of pointers to free memory blocks and their sizes. When you request one, it's given to you. Both read and write permissions here, but no execute.

有只读存储器(ROM).但是,这只是数据"部分.您不能在运行时更改它.当您创建一个const变量时,在内存中没有什么特别的事情发生.所有发生的事情是编译器将仅在其上创建某些指令.就是这样.x86不了解或不知道const的概念,它全在编译器中.

There is read-only memory(ROM). However, this is just the Data section. You can't alter it at runtime. When you make a const variable, nothing special happens to it in memory. All that happens is that the compiler will only create certain instructions on it. That's it. x86 has no knowledge or notion of const- it's all in the compiler.

这篇关于C/C ++程序的内存布局如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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