简体   繁体   中英

Where in memory are variables stored in C++?

If I have code like this:

int e;
int* f;

int main() {
    int a, b, c;
    int* d;
}

Where in memory are these variables stored? And, what's the problem with defining global variables (out of a function, like main in this case)?

There are actually no guarantees about placement of variables in memory, only behavior.

But on most (perhaps all) practical systems, e and f will be in the global data section, while a , b , c , d are in a mix of registers and on the call stack. Both f and d can point to anywhere in memory (well, with an MMU this isn't precisely true, they can point anywhere in the subset of memory which is mapped into the process).

a,b,c and d will exist on the stack. If you were to create an instance of an int (with malloc or new), that would go on the heap - but the pointer called 'd' would still exist on the stack.

e and f are allocated space in the memory space of the app, in the so-called 'Data segment' - see:

http://en.wikipedia.org/wiki/Data_segment

You also asked about stack size: that's configured by the compiler, see here:

http://msdn.microsoft.com/en-us/library/tdkhxaks(VS.71).aspx

Why do you think this matters? Note that in modern C++, having local pointer variables is extremely rare.

On a typical system, a, b, c and d will live on the stack. But that's an implementation detail, the language standard says nothing specific about that.

In general, a,b,c,d* will be defined on the program stack, though the C++ ISO standard doesn't specify where variables should be defined.

For your second question, the problem with global variables is that they have no protection. For a simple script/application relying on globals might be okay, but in libraries, and more complex code it produces a potential problem: any code can change the values of variables with global scope at anytime, leaving any code that relies on global variables in an undesired state.

Further, since variables are in the global scope, they're open to potential naming collisions if you make use of any other code that defines global variables with the same name.

The main point is the scope of the variables, which defines their lifetime. The variables outsie of main will be available for the duration of your program. The variables inside main will be availalbe for the duration of the main() function.

In practice, the function-scoped variables are usually allocated on the stack, while global variables are allocated on the heap, but this is an implementation detail.

The main problem with global variables is controlling access, and they can be difficult to work with in a multi-threaded program.

EDIT: As @FredOverflow points out below, the globals are typically allocated in the data area that is set up when the executable image is loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM