2

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?

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19

1 Answers1

2

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.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • Well, my `clang` compiler stores it next to constants as if it was in the `text` section. `gcc` does the same, but this one does not seem to follow the standard memory layout (somewhy constants have higher addresses than initialised global variables) – Kaiyakha Dec 06 '20 at 17:45
  • @Kaiyakha: The higher address indicates to me it's using `rodata`. – Joshua Dec 06 '20 at 17:46
  • @is it higher than `data`? – Kaiyakha Dec 06 '20 at 17:51
  • 1
    @Kaiyakha: Depends on link order, but it can be. – Joshua Dec 06 '20 at 17:52
  • What about `clang`? The addresses of `static const` (local and global) are near the addresses of string literals and other global constants and all of the addresses are much less than the addresses of variables from `data` segment (global vars initialised in place with non-zero values) – Kaiyakha Dec 06 '20 at 17:56
  • @Kaiyakha: I don't use clang. – Joshua Dec 06 '20 at 17:57
  • Well ok, what do you mean by `folded`? – Kaiyakha Dec 06 '20 at 17:58
  • @Kaiyakha: The compiler is allowed to place the value of a static const everywhere it is used and eliminate the variable. – Joshua Dec 06 '20 at 17:59
  • Finally I think `static const` is near other constants for both `clang` and `gcc`. For `clang` it's more likely that it is in the `text` segment because its address is the least along with the other constants, and for `gcc` it is probably in what you call `rodata`. Anyway, it is always besides other constants for the both compilers. – Kaiyakha Dec 06 '20 at 18:35
  • Basing where you think something is placed based on address is a bit silly, take a look at an object dump and remove the doubt. (objdump on *nix platforms or dumpbin.exe if using MSVC). Although taking an address itself may force creation of an object that would not otherwise be needed. – SoronelHaetir Dec 06 '20 at 20:07
  • @SoronelHaetir that's the point, I have neither *nix nor MSVC. I compile via command prompt on Windows – Kaiyakha Dec 06 '20 at 20:26
  • Your toolset produces a binary in _some_ format, very likely there is a tool that will dump the resulting object file, even if that is not a native format for the platform. For example, my cross-compile clang installations have llvm-objdump. – SoronelHaetir Dec 06 '20 at 22:22