This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
string variables and literals
- To: gcc at gcc dot gnu dot org, gcc-bugs at gcc dot gnu dot org
- Subject: string variables and literals
- From: Eric Lemings <elemings at uswest dot net>
- Date: Wed, 30 Aug 2000 21:32:08 -0600
Dear readers,
Recently, I was quite surpised to discover the following behavior of GCC
which I think is a bug.
Suppose you have four strings defined as four different types:
const char s1 [] = "array of null-terminated, const char";
const char* s2 = "pointer to array of null-terminated, const char";
char s3 [] = "array of null-terminated, non-const char";
char* s4 = "pointer to array of null-terminated, non-const char";
Can s1 or s2 be modified? Certainly not; they're declared as an array
and a pointer to const char, respectively. Can s3 be modified? Yes,
because it's declared as a non-const array of char. The compiler
initializes it with a copy of the string literal from read-only memory
(where all string literals are stored). Nothing wrong with that.
But what about s4? Can it be modified? It is not declared as a pointer
to const char. But guess what? That's exactly what it is. The
compiler seemingly does not recognize the fact that it should be a
pointer to an array of __non-const__ char. Just as s3 was initialized
with a copy of its string literal, shouldn't s4 be intialized with the
location of a copy of the string literal placed in some arbitrary part
of writable memory?
Thanks,
Eric.