The layout of strings in memory isn't documented or guaranteed in any way beyond the fact that the string will be there once the binary is loaded into memory.
It's quite plausible that APP_VERSION
may simply be initialized with a pointer that points partway into the contiguous sequence of bytes nullSWVER:v1.0.0
, which makes no difference to the visible behavior of your program, provided you don't invoke undefined behavior at some point. It's also quite likely that some subtle change to the compiler, libraries, or compile flags may change this layout unpredictably.
You shouldn't be relying on strings
to parse a binary anyway; chances are you'll see tons of cruft like function prologues that resemble strings, random strings from libc, etc.
It's also an implementation detail of typical executable formats that the strings are readable (common implementations place pages of .text and .data sections page-for-page into the executable file), but nothing about C requires that executable images are readable like that.
If you want to store version information in your binaries, you'll have to do it in an implementation-specific manner. Windows has version resources; for ELF files I believe the only approach would be a .note
section (or a section with some other custom name) and then reading it in a way that understands and respects sections, rather than just blindly scanning for strings.