nevin1@ihlpb.ATT.COM (Liber) (05/03/89)
In article <1814@leah.Albany.Edu> rds95@leah.Albany.Edu (Robert Seals) writes: >> In article <2747@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes: >> | 3 char *c; >> ... >> | 7 c = "somestring"; /* Nothing fancy, null-terminated. */ >Now I always thought this kind of thing was extremely bad form, but >I've recently seen a couple of code examples that use this. The form is fine. >One figures that if ``somestring'' is generated by the compiler somewhere, >then the assignment will work as expected, because the address of it will >be copied to ``c''. >But what if the compiler decides that ``somestring'' ^^^^^^^^ key word in this sentence >has finished it's duty to the program, and overwrites the space it took >with something else? If the *compiler* does this, then it is broken. Now, there are times when the *programmer* attempts to overwrite ``somestring'' (eg: *c = 'S';); this behavior is undefined bu pANS C. As it is written in section 3.1.4 (String Literals): "Identical string literals [...] need not be distinct. If the program attempts to modify a string literal [...], the behavior is undefined." This allows string literals to be put into read-only memory, etc. For stritly conforming programs, consider string literals to be arrays of *const* char (although their type technically is array of char, for backwards compatibility, and the reasons in my next paragraph). It is a common (non-portable) extension to have writable string literals. Even in this case, according to A.6.5.5 of the Appendix (which is considered unofficial) of the pANS C: "Identical string literals shall be distinct." meaning that ``somestring'' would still be intact, unless something pointing to that particular copy of ``somestring'' modified it. -- _ __ NEVIN ":-)" LIBER nevin1@ihlpb.ATT.COM (312) 979-4751 IH 4F-410 ' ) ) "I will not be pushed, filed, stamped, indexed, / / _ , __o ____ briefed, debriefed or numbered! My life is my own!" / (_</_\/ <__/ / <_ As far as I know, these are NOT the opinions of AT&T.
gwyn@smoke.BRL.MIL (Doug Gwyn) (05/03/89)
In article <1814@leah.Albany.Edu> rds95@leah.Albany.Edu (Robert Seals) writes: >But what if the compiler decides that ``somestring'' has finished it's >duty to the program, and overwrites the space it took with something else? We call that a "compiler bug". Strings have static storage duration; so long as one is still validly accessible via a pointer, it has to remain intact.