If a constant is defined globally, it goes to the text
segment. Local constants get pushed onto the stack
. Static variables get stored into either the data
or the bss
segment depending on if it is initialised in place and what it is initialised with.
What about static const
? This question links to another one which is only about static variables, not constants. I suppose static constants should be stored in the text
segment as read-only variables, but I'm not sure. Where is it commonly stored?
A
static const
may be folded at compile time. If it is not, then it is stored in thedata
orbss
segment as though it were astatic
(but other modules can't link to it). Storing it in thetext
segment is valid, but very few compilers do so. I've only seen it in embedded compilers where the RAM/ROM distinction matters. Newer toolchains often have arodata
segment that takes static and global constants.