string assignment in C

Knudsen knudsen at ihlpl.ATT.COM
Tue Oct 18 03:55:27 AEST 1988


In article <1414 at imagine.PAWL.RPI.EDU>, kyriazis at rpics (George Kyriazis) writes:
> My question is:  Are strings like " is:" volatile or not?
> When you say p2 = " is:", are you sure that the string will remain in
> memory or the optimizer will decide to put something else there since
> the string is basically a constant used only once??

Nope.  p2 will continue to point to " is:" unless you reassign p2,
which the compiler has no idea if or when you might do.
So as long as p2 exists, that constant string can't be recycled.
Worse yet, even if p2 is an auto variable, the " is:" has to stick around
in the text/code segment so that p2 can be initialized again if that
function is re-entered.

> I also have the idea that if you say
>         p1 = "abc";
>         p2 = "abc";
> p1 and p2 will have different value, since the strings are not the same
> (they have the same contents, but physically should be different).

Some compilers are fancy enough to detect identical string constants
and merge them, so indeed p1==p2 there.  I doubt many compilers
go this far, however.
-- 
Mike Knudsen  Bell Labs(AT&T)   att!ihlpl!knudsen
"Lawyers are like handguns and nuclear bombs.  Nobody likes them,
but the other guy's got one, so I better get one too."



More information about the Comp.lang.c mailing list