Background:
We are talking about modifying string literals. For example:
int main()
{
char * p = "Hello";
*p = 'Y';
}
celhellas@germanosnet.gr (Lefteris Laskaridis) wrote in message news:<a9ec9f6d.0401130339.2f68db87@posting.google. com>...[color=blue]
> "Ron Natalie" <ron@sensor.com> wrote in message news:<40030d2a$0$2641$9a6e19ea@news.newshosting.co m>...
>[color=green]
> > Purely by coincidence. One of those subtle problems with undefined
> > behavior[/color]
>
> it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.[/color]
By "correctly," you must mean "as expected"; because there isn't *one*
correct way of running when undefined behavior is involved. For
example, the program above aborts with a 'Segmentation fault' when
compiled with gcc 3.2 and run on Linux. (Using gcc 2.95 doesn't make
any difference.)
String literals are actually arrays of constant characters. The C++
standard allows non-const pointers to be initialized with string
literals to avoid breaking old C code. Attempting to modify the string
literal through such a pointer is undefined behavior.
So, actually
char const * p = "Hello";
would be the correct definition of a pointer pointing to the first
constant character of the string literal.
Ali