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)
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).
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
. A struct
is a class
whose default visibility is public. 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
}
精彩评论