Where are static constants stored?

1k Views Asked by At

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?

1

There are 1 best solutions below

12
On

A static const may be folded at compile time. If it is not, then it is stored in the data or bss segment as though it were a static (but other modules can't link to it). Storing it in the text 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 a rodata segment that takes static and global constants.