vegdahl@ogicse.cse.ogi.edu (Steve Vegdahl) (05/29/91)
The ANSI standard states that two identical string literals may share storage. Thus, whether the execution of the block: { char *a = "hello"; char *b = "hello"; printf("%s", a==b ? "YES" : "NO"); } prints "YES" is depends on the implementation. Question: can non-identical substrings share storage if one is a substring of the other, and they share a common "tail"? In other words, is it legal for an implementation to print "YES" when executing the following block: { char *a = "Say hello"; char *b = "hello"; printf("%s", (a+4)==b ? "YES" : "NO"); } I find nothing in the standard that gives a definitive answer to this question. Steve Vegdahl Adaptive Solutions, Inc.
gwyn@smoke.brl.mil (Doug Gwyn) (05/29/91)
In article <21996@ogicse.ogi.edu> vegdahl@ogicse.cse.ogi.edu (Steve Vegdahl) writes: >The ANSI standard states that two identical string literals may share storage. Actually it says that identical string literals need not be distinct. >Question: can non-identical substrings share storage if one is a substring >of the other, and they share a common "tail"? Sure. The standard doesn't allow a strictly conforming program to modify string literals, so this would be a safe implementation practice, and I would be greatly surprised if no C implementation were doing this. Surely any application that depended on nonoverlap would be depending on something that the standard does not guarantee.
henry@zoo.toronto.edu (Henry Spencer) (05/29/91)
In article <21996@ogicse.ogi.edu> vegdahl@ogicse.cse.ogi.edu (Steve Vegdahl) writes: >... standard states that two identical string literals may share storage. >Question: can non-identical substrings share storage if one is a substring >of the other, and they share a common "tail"? ... It appears to me that a very strict reading of the standard outlaws such sharing. The description (3.1.4) of what a string literal points to is in terms of each string literal initializing an anonymous array "just sufficient to contain the sequence", and the only exception granted is for identical string literals. The results of modifying one are undefined, but pointer comparison can still be used to notice such things, so the "as if" rule is not applicable. I doubt that this was the intent of X3J11, and I suggest that it is most unwise for code to depend on it. -- "We're thinking about upgrading from | Henry Spencer @ U of Toronto Zoology SunOS 4.1.1 to SunOS 3.5." | henry@zoo.toronto.edu utzoo!henry
sef@kithrup.COM (Sean Eric Fagan) (05/30/91)
In article <16293@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: >>Question: can non-identical substrings share storage if one is a substring >>of the other, and they share a common "tail"? >Sure. The standard doesn't allow a strictly conforming program to modify >string literals, so this would be a safe implementation practice, and I >would be greatly surprised if no C implementation were doing this. And that's what the old program, xstr, would do for you. (Well, they were still data, but it would set things up such that "World!" and "Hello, World!" shared some storage. At least the version I had did 8-)) -- Sean Eric Fagan | "I made the universe, but please don't blame me for it; sef@kithrup.COM | I had a bellyache at the time." -----------------+ -- The Turtle (Stephen King, _It_) Any opinions expressed are my own, and generally unpopular with others.