Where are variables in C++ stored?
Inside the RAM or the processor's cache?
Where are variables in C++ stored?
Inside the RAM or the processor's cache?
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.
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.
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.
Variables are stored:
new
or malloc
, etc.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.
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. :-)
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.
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.
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.
ya its absolutely correct the variable mostely stored in the Main memory
void main() { static int a=1,b=2,c=3,d=4,e=5;
printf("%d%d%d%d%d"); }
output 66088960187408648
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
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