As mentioned in the C++ Complete Reference, constant data are stored in ROM. But Local variables are stored inside the stack, which is in RAM. So if we declare local constant data, where is it stored? RAM or ROM?
-
2not RAM or ROM. Should be data segment and read only segment. – Matt Nov 21 '12 at 07:27
-
3This is completely **Implementation Defined**. You do not need to worry about where exactly they are stored.What you should understand is the contract of const correctness and adhere to it strictly. – Alok Save Nov 21 '12 at 07:31
-
As i know contant variable will never be stored inside the datasegment ..Please correct if i am wrong . – Viku Nov 21 '12 at 14:18
-
@Als - not so much implementation defined as implementation dependent. The language definition uses "implementation defined" for a particular requirement: implementations must document what they do for something that's implementation defined. Formally, the storage strategy is unspecified. – Pete Becker Nov 21 '12 at 15:24
3 Answers
The only true possible answer is "it depends".
C++ is one level of abstraction above the hardware, and produces code that may be "hosted" by some other software like an operating system.
The way it uses the hardware depends on what the hardware itself is and works and its managed, and how the compiler designer intended to use all those goodies.
The difference between constant and variables is that variables are designed (by the language designer) to be modified and retain their value, while constant are designed to never been modified.
The only thing C++ says about constant is that trying to modify them result in "undefined behavior": that is "the language designer say nothing about what may (or not) happen".
Compiler designer do what they can compatibly with the platform they work on. Some constants are stored in real ROM (think to micro-controllers), some in RAM with "write access denied" (so that an hardware trap is generated if you try to modify them: what happens depend on the OS: typically the trap is translated into an OS exception that may or not be supported by the language) some simply in RAM where no such "facility" exist, some other even inside the machine code instruction itself.
In any case, unless you are programming the hardware yourself, you sould not care of this details. And saying "constant can go in ROM" can be true (if the platform permits it) but "constant are stored in ROM" is just a plain lie.

- 20,229
- 2
- 46
- 63
The term "ROM" is usually reserved for hardware that is physically incapable of writes. Constants are still placed in what is physically "RAM" (otherwise, how could your program even load?)
Technically, there's little protection against writing in the spec...it's just undefined behavior if you do so.
As for where stuff is stored, there are some typical practices on current architectures. But the compiler is fairly free to make its own decisions. See this, for instance:

- 1
- 1

- 32,904
- 11
- 98
- 167
Local constant data stored on the stack at run time. Static local and global data usually stored in the read-only segment at compile time. However compiler can do some optimizations and store even local constants in in the read-only segment.

- 425
- 2
- 9