Why do same strings in a char* array have the same address?
Is this because of compiler optimization?
Example:
#include
#inc
It is called constant merging. It is enabled at higher levels of optimization, typically. The compiler simply takes all of the unique constant values and crunches them down. Good for memory usage and cache efficiency.
gcc has -fmerge-constants
or using -O and company
Other compilers may or may not do it. It is compiler specific.
Since it is about the easiest optimization operation to implement I would imagine all C++ compilers do it.
This is a perfect example of why:
but we see many questions about people (not yourself) observing they got away with modifying a constant string after casting away const.