views:

2866

answers:

11

Where are variables in C++ stored?

Inside the RAM or the processor's cache?

+1  A: 

Variables can be held in a number of different places, sometimes in more than one place. Most variables are placed in RAM when a program is loaded; sometimes variables which are declared const are instead placed in ROM. Whenever a variable is accessed, if it is not in the processor's cache, a cache miss will result, and the processor will stall while the variable is copied from RAM/ROM into the cache.

If you have any halfway decent optimizing compiler, local variables will often instead be stored in a processor's register file. Variables will move back and forth between RAM, the cache, and the register file as they are read and written, but they will generally always have a copy in RAM/ROM, unless the compiler decides that's not necessary.

Adam Rosenfield
Compilers for normal, non-embedded architectures do not place variables in "ROM."
Dan
ROM conventionally means memory that is written only during manufacture - const variables are still stored in the computer's RAM, but just aren't written to during the program execution
Chris Johnson
Stroustrup often talks about variables stored in ROM. As does the C++ Standards committee ( http://www.open-std.org/jtc1/sc22/wg21/docs/PDTR18015.pdf pg. 75). In reality, it's not physical ROM, but instead a section of the executable for data (in ELF it's the .text section).
Max Lybbert
+4  A: 

Variables in C++ are stored either on the stack or the heap.

stack:

int x;

heap:

int *p = new int;

That being said, both are structures built in RAM.

If your RAM usage is high though windows can swap this out to disk.

When computation is done on variables, the memory will be copied to registers.

Brian R. Bondy
A: 

depending on how they are declared, they will either be stored in the "heap" or the "stack"

The heap is a dynamic data structure that the application can use.

When the application uses data it has to be moved to the CPU's registers right before they are consumed, however this is very volatile and temporary storage.

Joe Basirico
+22  A: 

Variables are stored:

  • on the stack, if they're "automatic" function-local variables
  • on the heap, if they're allocated with new or malloc, etc.
  • in a per-process data area if they are global or static

This is all in RAM, of course. Caching is transparent to userspace processes, though it may visibily affect performance.

Compilers may optimize code to store variables in registers. This is highly compiler and code-dependent, but good compilers will do so aggressively.

Dan
Actually, variables are not stored in the heap. You may have a variable that points to something in the heap, but the variable itself will be in a register, on a stack, or be statically allocated.
Kristopher Johnson
Kristopher, a valid point. In the C++ definition, the variable is the pointer, not the pointed-to array, so you're right.
Dan
Upvoted but you should remove point 2.
Joe
Note that storing variables in registers is highly platform-dependent also. Different architectures have different numbers of registers, and not all registers are the same in all architectures.
David Thornley
+6  A: 
Mecki
+13  A: 

For C++ in general, the proper answer is "wherever your compiler decides to put them". You should not make assumptions otherwise, unless you somehow direct your compiler otherwise. Some variables can be stored entirely in registers, and some might be totally optimized away and replaced by a literal somewhere. With some compilers on some platforms, constants might actually end up in ROM.

The part of your question about "the processor's cache" is a bit confused. There are some tools for directing how the processor handles its cache, but in general that is the processor's business and should be invisible to you. You can think of the cache as your CPU's window into RAM. Pretty much any memory access goes through the cache.

On the other end of the equation, unused RAM sometimes will get swapped out to disk on most OSes. So its possible (but unlikely) that at some moments your variables are actually being stored on disk. :-)

T.E.D.
I understand that the compiler can decide to do whatever it wants. Are there compilers that currently do something very different from the usual (automatic=stack or registers, allocated=help etc) ?
+3  A: 

I think you are mixing up two concepts. One, how does the C++ language store variables in memory. Two, how does the computer and operating system manage that memory.

In C++, variables can be allocated on the stack, which is memory that is reserved for the program's use and is fixed in size at thread start or in dynamic memory which can be allocated on the fly using new. A compiler can also choose to store the variables on registers in the processor if analysis of the code will allow it. Those variables would never see the system memory.

If a variable ends up in memory, the OS and the processor chip set take over. Both stack based addresses and dynamic addresses are virtual. That means that they may or may not be resident in system memory at any given time. The in memory variable may be stored in the systems memory, paged onto disk or may be resident in a cache on or near the processor. So, it's hard to know where that data is actually living. If a program hasn't been idle for a time or two programs are competing for memory resources, the value can be saved off to disk in the page file and restored when it is the programs turn to run. If the variable is local to some work being done, it could be modified in the processors cache several times before it is finally flushed back to the system memory. The code you wrote would never know this happened. All it knows is that it has an address to operate on and all of the other systems take care of the rest.

Todd
In most modern systems, the stack is not fixed in size, but is automatically extended by the operating system on the occurrence of a page fault (because of an empty stack).
Paul de Vrieze
In your answer, it becomes very clear that two different things are going on: the language's "object model" and the RAM/SwapFile/Caching system. Nice answer!
xtofl
Hi Paul. Thanks for the comment. You're right that the stack is virtual memory and can be paged. My point was that it was fixed in size when allocated at thread start. This is governed by the linker.
Todd
+5  A: 

C++ is not aware of your processor's cache.

When you are running a program, written in C++ or any other language, your CPU will keep a copy of "popular" chunks of RAM in a cache. That's done at the hardware level.

Don't think of CPU cache as "other" or "more" memory...it's just a mechanism to keep some chunks of RAM close by.

Shmoopty
A: 

If local variables are stored on stack, how are these accessed. stack is FILO(First in last out). So last created variables should be retrieved first. for example,

int a, b, c; a = b + c;

in that case, c will be last variable on stack so does compiler need to pop up c, before accessing b.

sandip
Variables allocated on the stack are tracked via a pointer to their location on the stack. This is done with non-static variables declared in functions, and with arguments to functions.
Justin Smith
A: 

ya its absolutely correct the variable mostely stored in the Main memory

Vijay
A: 

include

void main() { static int a=1,b=2,c=3,d=4,e=5;

printf("%d%d%d%d%d"); }

output 66088960187408648

include

void main() { int a=1,b=2,c=3,d=4,e=5;

printf("%d%d%d%d%d"); } output 54321

this is in c language in the second program stack concept is working but why not in first if all variables within a function is stored in stack and why not it is working with float data type and double

include

void main() { float a=1,b=2,c=3,d=4,e=5;

printf("%f%f%f%f%f"); } output 512.0001232.0000000.0000000.0000006981019810192271279512.000000

yes all program follows stack concept c++ but what is the reason that it dosent works in c

anurag
Please use code formatting. this is pretty much unreadable
jalf