Russell said:
Hi,
If i have: char *str = "test" in one .c file, is it guaranteed
that str == "test" in any other .c file after linking?
(ie: there isn't multiple "test" strings stored in the final object)
There is no such guarantee, not even within just
one file:
char *p = "test";
char *q = "test";
assert (p == q); /* can fail */
However, some compilers (perhaps even "many") will
recognize this case and collapse both literals into just
one anonymous char[] array used as the target for both
pointers. Some will go even further:
char *p = "test";
char *q = "smartest";
assert (p == q + 4); /* can succeed */
Off-hand I don't know of an implementation that will
do this sort of thing across module boundaries. It'd
certainly be possible and there may be an implementation
that does it, but I haven't run into one (or rather, if
I have run into one I haven't noticed the fact).