Can I know where the volatile
variable is getting stored in the memory?
If i declare globally means where does it get stored in the memory?
volatile int a =10;
int main()
{
printf("Global A value=%d",a);
return 0;
}
If i declare locally inside the function means where does it get stored in the memory?
int main()
{
volatile int a =10;
printf("Local A value=%d",a);
return 0;
}
Does it get stored in Stack / RAM / Data segment ?
Please clarify my doubts.
There's no reason for a volatile variable to be stored in any "special" section of memory. It is normally stored together with any other variables, including non-volatile ones. If some compiler decides to store volatile variables in some special section of memory - there's nothing to prevent it from doing so.
Volatile fields are instance or class (static) variables and are stored in the heap.
Variables declared as volatile are not cached in registers between operations, so that if something writes to it outside the current thread of execution, the next time the current thread of execution needs it it will be read from the memory again each time it is used.
As per the memory layout of C program, constant variables are stored in the; Initialised data segment of the RAM. Since RAM is mostly volatile, then the constants are stored in flash memory and may or may not be copied to RAM during initialisation.
volatile
just tells the compiler it can't cache the value of the variable in a register—it doesn't change where it gets allocated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With