string assignment in C

Mark Nagel nagel at paris.ics.uci.edu
Mon Oct 17 12:13:01 AEST 1988


In article <1414 at imagine.PAWL.RPI.EDU>, kyriazis at rpics (George Kyriazis) writes:
|In article <6777 at chinet.chi.il.us> john at chinet.chi.il.us (John Mundt) writes:
|>Each string,
|>"first" and " is:" are reserved by the compiler as unnamed strings
|>somewhere in memory. ......
|
|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??

Of course not!  That would be analogous to having the 2 in:

        x = 2;

change at some point in the program since it is "basically a constant."  
Also, *all* code has the potential for being executed more than once 
through function calls (well, not global variable initialization, but 
all code in functions).

|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).
|Is that a right assumption?

No.  Some compilers will treat them as different objects (i.e. you get
two distinct pointer values for the two string constants).  Other's will
optimize the program's space usage by sorting and uniq'ing all of the
strings so that different string constant references all have the same
physical address.  This is fine since constant strings should be
read-only objects.

Mark D. Nagel
  UC Irvine - Dept of Info and Comp Sci | The probability of someone
  nagel at ics.uci.edu             (ARPA)  | watching you is proportional to
  {sdcsvax|ucbvax}!ucivax!nagel (UUCP)  | the stupidity of your action.



More information about the Comp.lang.c mailing list