[英]Stack memory allocation of variables in c++
How does the c++ compiler organize the variables that are initialized in a function to store them so the computer will find them the fastest way? C ++编译器如何组织在函数中初始化的变量以存储它们,以便计算机以最快的方式找到它们?
I understand that the compiler puts them one after an other on the stack but there has to be some logic behind it, I was searching Google for hours on end but I could not find anything. 我知道编译器将它们一个接一个地放在堆栈上,但是背后必须有一定的逻辑,我在Google上搜索了好几个小时,但找不到任何东西。
For example: 例如:
int main()
{
float a;
int b;
char c;
double d;
}
This should occupy more memory than the one below because of the way the c++ compiler is storing in the memory. 由于c ++编译器在内存中的存储方式,因此它应该比下面的内存占用更多的内存。
The exact bits used are the same, of course, but they should be stored in a more efficient order in the example below. 当然,所使用的确切位相同,但是在下面的示例中,它们应该以更有效的顺序存储。 Where in memory would these variables be stored by the compiler in the next example?
在下一个示例中,这些变量将由编译器存储在内存中的什么位置? As far as I understood a variable is always stored on a block such that
(logical number) % (number of bytes the datatype) = 0
据我了解,变量始终存储在块上,以使
(logical number) % (number of bytes the datatype) = 0
int main()
{
char c;
int b;
float a;
double d;
}
在C ++标准中,不要求将自动 (您称为stack )变量按特定顺序或位置进行布局(只要满足对齐要求)。
This should occupy more memory than the one below because of the way the c++ compiler is storing in the memory.
由于c ++编译器在内存中的存储方式,因此它应该比下面的内存占用更多的内存。
Not really, the stack memory consumed by both functions should be the same for any sane optimizing compiler ... Modern C++ Compilers are really aggressive at certain optimizations. 并非完全如此,对于任何明智的优化编译器 ,这两个函数消耗的堆栈内存都应该是相同的。现代C ++编译器在某些优化方面确实具有攻击性。
Other than suitable alignments, C++ does not impose memory address ordering for automatic variables in functions. 除了合适的对齐方式外,C ++不会对函数中的自动变量强加内存地址顺序。 While that is true, the observable behavior of the program must not be changed by the compiler.
确实如此,编译器不得更改程序的可观察行为 。
I guess you are trying to talk about struct
s and class
es where the memory layout and address ordering of variables are as declared . 我猜你是想谈
struct
S和class
,其中的内存布局和变量地址排序ES 所申报 。
How does the c++ compiler organize the variables that are initialized in a function to store them so the computer will find them the fastest way?
C ++编译器如何组织在函数中初始化的变量以存储它们,以便计算机以最快的方式找到它们?
In practice, every access to an automatic variable in C++ is a simple pointer offset with respect to the stack pointer 1 (except variables the compiler placed directly in a register). 实际上,对C ++中的自动变量的每次访问都是相对于堆栈指针 1的简单指针偏移量(编译器直接放置在寄存器中的变量除外)。 Additionally, to speed things up to such automatic variables (in no order):
此外,为了使事情加速到此类自动变量(无顺序):
The Compiler eliminates dead variables 编译器消除了死变量
The compiler will find the best order to store each of them to meet suitable alignment 编译器将找到最佳顺序来存储它们中的每一个,以达到适当的对齐
The compiler may use the CPU register directly depending on what it's Register Allocation algorithm decides 编译器可以根据其寄存器分配算法的决定直接使用CPU 寄存器
The compiler may lump certain variables together into a vector register and use vector instructions provided it will yield correct results. 编译器可以将某些变量集中到向量寄存器中,并使用向量指令,前提是它将产生正确的结果。
...and so much more. ...等等。
1 : Even the stack pointer is aligned by most compilers. 1 :大多数编译器甚至都将堆栈指针对齐 。
Registers and optimizations. 注册和优化。
The most efficient access of variables is to eliminate them. 变量的最有效访问是消除它们。 Many compilers will optimize away variables that are not used.
许多编译器会优化未使用的变量。
If a variable is used locally, the compiler may decide to place the variable in a register. 如果在本地使用变量,则编译器可以决定将变量放置在寄存器中。 Registers are the most efficient access for variables.
寄存器是对变量的最有效访问。
A stack is a convenient data structure for allocating local variables. 堆栈是用于分配局部变量的便捷数据结构。 The compiler can destroy variables on the stack by changing the stack pointer .
编译器可以通过更改堆栈指针来破坏堆栈上的变量。 Implementations that use a stack often have a pointer to the top of the stack (where the next variable is allocated).
使用堆栈的实现通常有一个指向堆栈顶部的指针(分配了下一个变量)。 Allocation is as simple as adjusting the pointer by a constant (which is an arithmetic operation).
分配就像通过常数调整指针一样简单(这是一种算术运算)。
Remember, there is no requirement that a compiler use a stack. 请记住,不要求编译器使用堆栈。
谢谢您的帮助,我现在也对我正在寻找的逻辑提供了不错的描述,对于陷入困境的任何人,我都会在此处发布链接: http : //www.tenouk.com/Bufferoverflowc/Bufferoverflow2.html http ://www.tenouk.com/Bufferoverflowc/Bufferoverflow2a.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.