什么决定了变量在内存中的保存顺序?

发布于 2024-12-11 08:46:05 字数 382 浏览 0 评论 0原文

最近在学习C,遇到了一些我似乎找不到好的答案。

在我的代码中,我可能有:

struct randomStruct a;
struct secondStruct array[5];
struct structyStruct q = { 17, "Hey yo", {123,123}};

但在内存中,它们可能存储为:

q, a, array

我的意思是,内存中 q 的地址低于 a 的地址,a 的地址低于数组的地址。

什么决定了变量在内存中保存的顺序?

(如果您需要更多信息/实际可运行代码,请告诉我)

提前致谢!

I've been learning about C recently, and I've encountered something which I can't seem to find a good answer to.

In my code I might have:

struct randomStruct a;
struct secondStruct array[5];
struct structyStruct q = { 17, "Hey yo", {123,123}};

But in memory, they might be stored as:

q, a, array

What I mean here is that q's adress in memory is lower than a's, which is lower than array's.

What determines the order in which variables are saved in memory?

(If you need more information/actual runnable code, please let me know)

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2024-12-18 08:46:05

您所看到的情况是由于 方向堆栈增长

该标准没有提及自动变量的地址。因此,变量地址之间不需要存在关系。

What you are seeing happens because of the direction of the stack growth.

The standard says nothing whatsoever about addresses of automatic variables. Thus it's not required that a relation between variables' addresses exists.

柒七 2024-12-18 08:46:05

C 标准指定了结构中项目的内存顺序,并且许多(也许是所有)实现显式指定了可变参数函数参数的内存顺序,但自动变量和静态变量的顺序应被视为编译器可以任意或无缘无故地随意排列。许多编译器使用哈希表作为标识符,并且至少一些编译器会根据变量的哈希码来排列变量,这使得变量的放置实际上是随机的。

此外,某些处理器的设计使得某些变量放置将比其他处理器产生更快的执行速度。例如,某个微控制器的编译器可能会确定将两个变量放置在彼此 256 字节以内将允许它将其中一个变量的地址加载到寄存器中,然后使用短位移寄存器相对寻址来访问这两个变量;即使今天的编译器不会进行这种优化,也没有理由相信明天的编译器不会。

如果您关心内存中变量的相对排列,那么您必须以供应商特定的方式标记变量(通常使用#pragma指令)来控制它们在内存中的放置方式,或者将所有感兴趣的变量放入结构。在某些情况下,使用 #define 宏允许通过看起来像变量名称的内容来引用结构项可能会有所帮助;这样做的最大警告是 #define 宏不遵守任何正常的范围规则;因此,必须确保“变量”名称不会在程序中的任何位置用作任何类型的标识符。

The C standard specifies the memory order of items within a struct, and many (perhaps all) implementations explicitly specify the memory order of parameters to variadic functions, but the order of automatic and static variables should be regarded as something which a compiler may arbitrarily or randomly arrange without rhyme or reason. Many compilers use a hash table for identifiers, and at least some compilers would arrange variables according to their hash codes, which made variable placement effectively random.

Further, some processors are designed so that certain variable placements will yield faster execution than others. For example, a compiler for a certain microcontroller may determine that placing two variables within 256 bytes of each other will allow it to load the address of one into a register and then use short-displacement register-relative addressing to access both; even if today's compiler wouldn't make that optimization, there's no reason to believe tomorrow's won't.

If you care about the relative arrangement of variables in memory, then you must either mark the variables in a vendor-specific way (typically using #pragma directives) to control how they are placed in memory, or else place all variables of interest into a struct. In some cases, it may be helpful to use #define macros to allow the struct items to be referred to by what look like variable names; the biggest caveat with doing this is that #define macros do not obey any normal rules of scope; consequently, one must be certain that the "variable" names are not used as any sort of identifier anywhere in the program.

甜宝宝 2024-12-18 08:46:05

在C 语言中,单独的对象(即不属于同一结构或数组的对象)在内存中没有顺序。指针的比较和减法仅针对指向公共数组元素的指针定义。

In the C language, separate objects (i.e. objects which are not part of the same structure or array) do not have an order in memory. Comparison and subtraction of pointers are defined only for pointers to elements of a common array.

花落人断肠 2024-12-18 08:46:05

所有这些项目都在堆栈上声明,这就是您看到这一点的原因。

请参阅堆栈上的 wiki。

http://en.wikipedia.org/wiki/Stack_(abstract_data_type)

然后查看此 内存布局。应该回答你的问题。

关于 rel="nofollow">http://discussknowhow.blogspot.com/2008/08/memory-layout-of-c-program-stack-wise.html

All those items are declared on the stack, which is why you are seeing this.

See the wiki on stacks.

http://en.wikipedia.org/wiki/Stack_(abstract_data_type)

Then checkout this on memory layout. Should answer your question.

http://discussknowhow.blogspot.com/2008/08/memory-layout-of-c-program-stack-wise.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文