Where are constant volatile variables stored in c ?

10,177

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

  1. Local variables and function frame - stack

  2. Global and static variables if uninitialized - .bss block start by symbol

  3. Global and static variables if initialized - data segment

  4. Environment variables and arguments - on top of the stack

  5. Dynamic data allocation - heap

  6. Const - ROM

  7. Volatile - no storage

  8. Register - cpu register

  9. 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.

Share:
10,177
suraj
Author by

suraj

Updated on June 05, 2022

Comments

  • suraj
    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?