jeffa@hpmwtd.HP.COM (Jeff Aguilera) (10/27/89)
C++ generates incorrect code for the following program. The problem exists
under versions 1.2, 2.0 beta, and 2.0. Correct code is emitted when the inline
specifier is deleted.
/*********************************************************************/
//inlinebug.c
const char* null(const char* p) {
if (p) while(*p) ++p;
return p;
}
inline int length(const char* p) { return null(p)-p; }
int snafu() { return length("oops"); }
/*********************************************************************/
C++ translates this as follows:
/* @(#)<<AT&T C++ Translator 2.00 06/30/89>> */
/* < inlinebug.c > */
...
char* null__FPCc(__0p) char* __0p; {
if (__0p) while ((*__0p)) ++__0p;
return __0p;
}
int snafu__Fv() { return ((null__FPCc(((char*)"oops"))-((char*)"oops"))); }
Although ANSI C permits the two instances of "oops" to share the same read-only
address, this is not required, nor is it traditional:
A2.6 Strings (Kernighan and Ritchie 2nd ed, p.194)
A string literal, also called a string constant, is a sequence of characters
surrounded by double quotes, as in "...". A string has type ``array of
characters'' and storage class static, and is initialized with the given
characters. Whether identical string literals are distinct is
implementation-defined, and the behavior of a program that attempts to
alter a string literal is undefined.
2.5 Strings (Stroustrup, p.247):
A string is a sequence of characters surrounded by double quotes, as in
"...". A string has type ``array of characters'' and storage class
static, and is initialized with the given characters. All strings, even
when written identically, are distinct.
2.5 Strings (Kernighan and Ritchie, p.181)
A string is a sequence of characters surrounded by double quotes, as in
"...". A string has type ``array of characters'' and storage class
static, and is initialized with the given characters. All strings, even
when written identically, are distinct.
---
jaa