Is how to lay out the constant string compiler dependent?

Is how to lay out the constant string compiler dependent?

Post by Quentin Yu » Fri, 18 May 2007 12:13:53



I always consider that the constant character strings of which
literal value are the same lay out at the same logic address, in
another words, every constant character string have only one copy in
the program. My view base on that if it doesn't that, you lose the
memory of the string that you have used except store the address in a
pointer, this is a form of memory leak although it doesn't really eat
the memory as the general memory leak. The x86 machine always support
my view:-)
But recently someone told me that the addresses of the same character
strings are not the same in SunOS, so it seems that how to lay out the
constant strings is complier dependent. If it's true, I wonder the
reason why my view is wrong? Both informations and links are
appreciated. Thanks to all!

Best Regards
 
 
 

Is how to lay out the constant string compiler dependent?

Post by kuype » Sat, 19 May 2007 00:03:45


The standard does not require that the character arrays corresponding
to identical string literals must share the same memory. The standard
is, in fact, written with the attitude that the corresponding
character arrays are naturally distinct objects, but it warns the
reader that they allowed to be the same, as an optimization. This is,
however, a very popular optimization.

Relevant clauses inculde 6.4.5p6: "It is unspecified whether these
arrays are distinct ..." and 6.5.2.5p8: "String literals, and compound
literals with const-qualified types, need not designate distinct
objects."

Note that in some cases strings can overlap in memory, even when
they're not identical. For instance, it might be the case that

"Hello world!" + 6 == "world!"

has a value of "true".

 
 
 

Is how to lay out the constant string compiler dependent?

Post by David R Tr » Sat, 19 May 2007 01:20:09


ISO C states that a compiler is free to combine equivalent static
string
constants so they are stored at the same (over even overlapping) areas
of memory at runtime. But compilers are not required to do so to be
conforming.

Even compilers that combine like strings might only do so when
given the appropriate optimization options at compile time, so the
assumption may not even be valid for a given compiler.

If you're writing portable code, you cannot assume that any two alike
string constants have the same runtime address. You also cannot
assume that they have different runtime addresses.

-drt