[英]Are global structs allocated on the stack or on the heap?
I am writing in an environment where I am not allowed to allocate new memory after program startup, nor am I allowed to make operating system calls. 我在一个环境中写作,我不允许在程序启动后分配新的内存,也不允许我进行操作系统调用。 In tracking down a page fault error (likely caused by inadvertently violating one of the above) the question occurs to me (since this bit me in the butt with std strings) 在追踪页面错误错误(可能是由于无意中违反了上述之一)时,问题就发生在我身上了(因为这个位于我的stt字符串中)
Is a global/local struct allocated on the stack or heap? 是在堆栈还是堆上分配的全局/本地结构? For example: 例如:
If this statement is in the global scope 如果此声明在全局范围内
struct symbol {
char blockID;
int blockNum;
int ivalue;
double fvalue;
int reference;
bool isFloat, isInt, isRef;
int symbolLength;
} mySymbol;
where is the memory for it allocated? 分配的内存在哪里?
It's implementation-defined (the C++ standard doesn't really talk about stack and heap). 它是实现定义的(C ++标准并没有真正谈论堆栈和堆)。
Typically, objects with static storage duration (such as globals) will end up in a special segment of address space that is neither stack nor heap. 通常,具有静态存储持续时间的对象(例如全局变量)将最终位于地址空间的特殊段中,该段既不是堆栈也不是堆。 But the specifics vary from platform to platform. 但具体情况因平台而异。
In C++, unlike in C#, struct
makes few differences with class
. 在C ++中,与C#不同, struct
与class
几乎没有区别。 A struct
is a class
whose default visibility is public. 一个struct
是一class
,其默认的知名度是公众。 Whether the allocation is performed on the stack or in the heap depends on the way you allocate your instance 分配是在堆栈上还是在堆中执行取决于您分配实例的方式
class A;
void f()
{
A a;//stack allocated
A *a1 = new A();// heap
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.