Topic: char* optimization?


Author: Steve Clamage <clamage@eng.sun.com>
Date: Wed, 22 May 2002 16:13:24 CST
Raw View
On Mon, 20 May 2002, Sebastian Wagner wrote:
>
> > You are not allowed to change szMyString because it points to const
> memory.
>
> Now I see, didn't know that. Does the same apply to
>
> char str[] = "const"
>
> ? I guess so...

No, because str is not a pointer. It is an array of 6 (non-const)
chars that are initialized with a copy of the literal string.
The meaning is the same as if you had written
 char str[] = { 'c', 'o', 'n', 's', 't', 0 };

Pointers and arrays are quite different things.

Confusion arises because use of an array in an expression causes
the array value to be converted to a pointer to its first element.
Programmers sometimes incorrectly think of arrays and pointers
as somehow being equivalent. They are not.

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 20 May 2002 19:35:07 GMT
Raw View
In article <iirgeuof3cpt3v14hjaavn4jdrapdae83v@4ax.com>, Jack Klein
<jackklein@spamcop.net> writes
>In C, compilers are allowed reuse string literals.  For example:
>
>char *s1 = "foo";
>char *s2 = "myfoo";

and so is C++ (allowed to reuse part or all of a string literal)

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: drew@revolt.poohsticks.org (Drew Eckhardt)
Date: Mon, 20 May 2002 21:29:59 GMT
Raw View
In article <acartu$pou$02$1@news.t-online.com>,
Sebastian Wagner <Seb.Wagner@gmx.net> wrote:
>
>> You are not allowed to change szMyString because it points to const
>memory.
>
>Now I see, didn't know that. Does the same apply to
>
>char str[] = "const"

No.

char str[] = "literal" allocates a chunk of memory for str that's big enough
to hold "literal" and then copies "literal" into it.

--
<a href="http://www.poohsticks.org/drew/">Home Page</a>
Some people consider posting to Usenet a solicitation for their advertising.
I consider such mail implicit agreement to entertain me via phone and e-mail.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Ken Alverson" <Ken@Alverson.com>
Date: Tue, 21 May 2002 16:39:41 GMT
Raw View
"Sebastian Wagner" <Seb.Wagner@gmx.net> wrote in message
news:ac86c7$qfr$04$1@news.t-online.com...
>
> char* szMyString = "foo";
> char* szMyString2 = "foo";
>
> So if I changed szMyString2, szMyString would change as well.

The problem here is that "foo" is really itself a pointer into your constant
pool; its type is really const char*.  For backwards compatibility purposes,
the expression "<char*> = <const char*>" is allowed silently when the right
side is a string literal, but the effect is as if you had explicitly
const_cast<>ed the result, that is to say, it is undefined if you write to
it.

Since "foo" is in reality a const char*, it is entirely legal for the
compiler to overlap the two, but whether or not that happens is
implementation specific.

Ken


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Sebastian Wagner" <Seb.Wagner@gmx.net>
Date: Sun, 19 May 2002 23:19:02 GMT
Raw View
Recently someone told me of a rule I couldn't reproduce on my compiler
(VC6). He said that, while

char[] szMyString = "foo";
char[] szMyString2 = "foo";

asszures that the the compiler allocates 2 different memory blocks, the
compiler *could* make just one if I wrote code  like this (* instead of []):

char* szMyString = "foo";
char* szMyString2 = "foo";

So if I changed szMyString2, szMyString would change as well.

As I said, I couldn't reproduce it. Still, I'd be interested to know for
sure if there is a rule a C compiler could do this. Anybody knows more?

Thanks in advance,
    Sebastian


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Jack Klein <jackklein@spamcop.net>
Date: Mon, 20 May 2002 17:32:18 GMT
Raw View
On Sun, 19 May 2002 23:19:02 GMT, "Sebastian Wagner"
<Seb.Wagner@gmx.net> wrote in comp.std.c++:

>
> Recently someone told me of a rule I couldn't reproduce on my compiler
> (VC6). He said that, while
>
> char[] szMyString = "foo";
> char[] szMyString2 = "foo";
>
> asszures that the the compiler allocates 2 different memory blocks, the
> compiler *could* make just one if I wrote code  like this (* instead of []):
>
> char* szMyString = "foo";
> char* szMyString2 = "foo";
>
> So if I changed szMyString2, szMyString would change as well.
>
> As I said, I couldn't reproduce it. Still, I'd be interested to know for
> sure if there is a rule a C compiler could do this. Anybody knows more?
>
> Thanks in advance,
>     Sebastian

String literals are a special case in C and C++, but a little
different in each.

In C, compilers are allowed reuse string literals.  For example:

char *s1 = "foo";
char *s2 = "myfoo";

In this case, there might be only one array of 6 characters, "myfoo"
with its terminating '\0' character.  s2 could contain the address of
the 'm' character, and s1 could be initialized to s2 + 2, that is
point to the 'f' in the same array.

There are some implementations of C, especially for embedded systems
or other platforms where memory may be limited, that such reuse is
standard or offered as an option.

In any case, attempting to modify a string literal in C invokes
undefined behavior.

In C++, string literals have the type array of constant char.  They
are explicitly constant.  For backwards compatibility, you are allowed
to assign the address of a string literal (array of constant char) to
a pointer to ordinary, that is non constant, char.

In C++, as in C, attempting to modify any object that was actually
defined as const invokes undefined behavior.

In either language, when you define arrays and initialize them with
string literals, each array must be a distinct object with a different
address.  So:

char ca1[] = "foo";
char ca2[] = "foo";

Each of these arrays is a different object and must be in a different
memory location.  Even if they are both constant:

const char ca1[] = "foo";
const char ca2[] = "foo";

Each of the arrays is a separate object and must have its own memory
space.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Sebastian Wagner" <Seb.Wagner@gmx.net>
Date: Mon, 20 May 2002 17:33:02 GMT
Raw View
> You are not allowed to change szMyString because it points to const
memory.

Now I see, didn't know that. Does the same apply to

char str[] = "const"

? I guess so...

Sebastian


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 20 May 2002 17:48:13 GMT
Raw View
In article <ac86c7$qfr$04$1@news.t-online.com>, Sebastian Wagner
<Seb.Wagner@gmx.net> writes
>Recently someone told me of a rule I couldn't reproduce on my compiler
>(VC6). He said that, while
>
>char[] szMyString = "foo";
>char[] szMyString2 = "foo";
>
>asszures that the the compiler allocates 2 different memory blocks,
Indeed because the string literal "foo" is just an initialiser and is
copied into the array, so you get two arrays that initially have
identical content.

>the
>compiler *could* make just one if I wrote code  like this (* instead of []):
>
>char* szMyString = "foo";
>char* szMyString2 = "foo";
>
>So if I changed szMyString2, szMyString would change as well.
>
>As I said, I couldn't reproduce it. Still, I'd be interested to know for

"foo" is a string literal, for a long time (back before C had a
standard) a string literal has been semantically unwrittable (i.e. an
attempt to write to it produces undefined behaviour). However, the
compiler is NOT required to collapse identical string literals into one,
it is just allowed to do so. In fact it can handles string literals any
way it likes as long as you get the correct behaviour when reading them,
or part of them.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 20 May 2002 19:19:36 GMT
Raw View
In article <acartu$pou$02$1@news.t-online.com>, Sebastian Wagner
<Seb.Wagner@gmx.net> writes
>> You are not allowed to change szMyString because it points to const
>memory.
>
>Now I see, didn't know that. Does the same apply to
>
>char str[] = "const"
>
>? I guess so...

No, you still do not understand.

char* s_ptr = "const";

makes s_ptr point to the place where the literal is stored and that
place is deemed immutable (some of us like to think of it as a pure
value)

char s[] = "const"

results in storage for a sufficiently large array of char being
allocated as appropriate (depending on the scope of the definition) and
then filling that storage with a COPY of "const". There is no reason the
copy should not be changed.

>
>Sebastian

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]