English 中文(简体)
C/C++方案的记忆如何?
原标题:How is the memory layout of a C/C++ program?
  • 时间:2010-06-20 18:11:19
  •  标签:
  • 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?

是否只有记忆科可用? 当我有最复杂的变数时,实际发生的情况是,汇编者只读一个记忆科,或者只读一个记忆科。

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

任何很好的参考/文章?

我认为,记忆科和布局是独立的,与汇编者有更大的关系。 是否在所有的顾问办公室都出现过Stack、Heap、法典、数据[启动、未启动]部分? 当有静态数据时,汇编者知道什么是静态的,接下来是什么,会做什么? 它是管理该方案的汇编者,应当知道什么是正确的? 所有汇编者都应遵守共同标准?

问题回答

实际确定C++记忆布局的情况很少。 然而,大多数现代的非洲顾问办公室采用一种相当类似的制度,而各部门根据许可分开。

《刑法》已经执行许可。 其他部分没有。 在“视窗”应用中,你只能把一些本地的法典放在 st子上和执行。 林丹具有同样的功能,它存在于x86的建筑中。

数据是部分结果(例如,等等)的数据,但不能书写。 这一节基本上是字面。 本节只读许可。

这两个部分是由此产生的档案的一部分。 食肉和肥皂被分配时间,而不是被铺开。

食肉类基本上是一片大片(1MB),许多编纂者为之提供了一种环境)分配。 编辑为你们管理。

记忆犹新是,本组织通过某种进程返回了你。 通常,蒸汽是点器的肥皂(数据结构),可以免费传承。 当你提出要求时,它就给了你。 两者都读写许可,但没有执行。

只读存储器。 然而,这只是数据部分。 您可以暂时改变。 当你做一个最复杂的变数时,它就没有发生任何特殊情况。 所发生的一切是,汇编者只能就此发出某些指示。 这就是说。 x86在汇编者中没有任何知识或概念。

AFAIK:

Stack/Heap do they use the same section of memory as they can grow independently?

它们可以无休止地增长。

该法典部分的内容是什么?

储存编码和综合数据只读部分。

When I have a function is it a part of the stack or the code section?

该职能的定义(代码)将载于《破产管理条例》。 每一份呼吁的论据都用在一边。

Also what is this initialized/uninitialized data segment?

数据部分储存了全球/统计变量。

Are there read only memory section available?

代码部分。 我假定一些职业介绍所(might<>em>)为创建只读的行业提供价格。

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?

数据部分。

我在阅读C/C++的记忆布局时也处于同样的两难境地。 在这方面,我随后把这些问题清除起来。

http://www.geeksforgeeks.org/memory-layout-of-c-program/

此处添加了主要示例:

“entergraph

我希望这有助于找到类似问题的答案。

(说明:下文适用于六氯环己烷)

这一过程的 st脚和ap积存在于记忆过程的“ame”部分。 差距和差距在相互之间发展(最初,当进程启动时,梯子占据了整个地区,可以由 st和肥皂组合而占据;每个记忆分配(小型/免费/新的/支离式)可以推动 st与 he之间的边界。 黑海经合组织科也位于同一个由本组织分配的工地上,它有自己的部分,包含全球变量。 只读的数据在数据栏中,包括插图字等内容。 例如,如果你的法典有以下几条内容:

char tmpStr[] = "hello";

之后,包含“hello”的源代码部分将居住在数据部分。

一本关于此内容的良好、彻底的书籍是E.Briant计算机系统。

作为答复的增编,本文引自。 GotW ,将一些主要记忆领域分类(指的是自由建筑之间的区别,这是我通常称为肥皂,而实际肥皂则是通过小型/免费管理的一部分)。 该条是过时的,因此我不知道它是否适用于现代C++;到目前为止,我没有发现直接矛盾。

Const Data The const data area stores string literals and other data whose values are known at compile time. No objects of class type can exist in this area. All data in this area is available during the entire lifetime of the program. Further, all of this data is read-only, and the results of trying to modify it are undefined. This is in part because even the underlying storage format is subject to arbitrary optimization by the implementation. For example, a particular compiler may store string literals in overlapping objects if it wants to.

Stack The stack stores automatic variables. Typically allocation is much faster than for dynamic storage (heap or free store) because a memory allocation involves only pointer increment rather than more complex management. Objects are constructed immediately after memory is allocated and destroyed immediately before memory is deallocated, so there is no opportunity for programmers to directly manipulate allocated but uninitialized stack space (barring willful tampering using explicit dtors and placement new).

Free Store The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object s lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object s nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

Heap The heap is the other dynamic memory area, allocated/freed by malloc/free and their variants. Note that while the default global new and delete might be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

Global/Static Global or static variables and objects have their storage allocated at program startup, but may not be initialized until after the program has begun executing. For instance, a static variable in a function is initialized only the first time program execution passes through its definition. The order of initialization of global variables across translation units is not defined, and special care is needed to manage dependencies between global objects (including class statics). As always, uninitialized proto- objects storage may be accessed and manipulated through a void* but no nonstatic members or member functions may be used or referenced outside the object s actual lifetime.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?