Where are constant volatile variables stored in c ?
Solution 1
A const volatile
variable means that your C program can't legally change it, but something else can. It would be logical to place this variable in RAM, but the compiler won't complain if you tell it (via a linker script or a similar option) to place in ROM. It may also be useful to locate this variable where some memory-mapped device is, e.g. a read-only timer counter register or an ADC output register.
Solution 2
Volatile
has noting to do with where the variable is stored. It just tells the compiler to read the variable from memory every time to avoid any optimization that compiler might perform for that variable.
Solution 3
Local variables and function frame - stack
Global and static variables if uninitialized - .bss block start by symbol
Global and static variables if initialized - data segment
Environment variables and arguments - on top of the stack
Dynamic data allocation - heap
Const - ROM
Volatile - no storage
Register - cpu register
Const volatile - in the same place as const storage
Solution 4
const
variables for microcontroller applications are most likely stored in flash ROM. The only time they are stored in RAM is when they are evaluated in runtime, such as const parameters to functions. Or when you are doing some debug build executing from RAM.
volatile
has nothing to do with where variables are stored, as explained in other answers.
suraj
Updated on June 05, 2022Comments
-
suraj about 2 years
In which section is constant volatile variable stored in c.? In micro-controllers we should put such kind of variables in RAM. Right?