Re: RFC: String cleanup
- From: Owen Taylor <otaylor redhat com>
- To: Benoît Dejean <benoit placenet org>
- Cc: gtk-devel-list gnome org, Daniel Marjamäki <daniel marjamaki comhem se>, Xavier Bestel <xavier bestel free fr>
- Subject: Re: RFC: String cleanup
- Date: Fri, 16 Dec 2005 10:47:13 -0500
On Fri, 2005-12-16 at 16:37 +0100, Beno�Dejean wrote:
> Le vendredi 16 d�mbre 2005 �6:16 +0100, Xavier Bestel a �it :
> > On Fri, 2005-12-16 at 16:11, Beno�Dejean wrote:
> >
> > > > Changing from:
> > > > [const] char *str
> > > > to:
> > > > [const] char str[]
> > >
> > > Isn't that exactly the same ?
> >
> > const char str[] is a string of chars (and a symbol pointing of them).
> >
> > const char *str is a string of char and a pointer on them (and a
> > symbol).
>
> I don't get it. To me, it's is exactly the same, the [] notation giving
> extra information that str is going to be used like an array.
If you tried using it in a function, then GCC may have optimized out
the difference because it could see all uses of the variable
But note that if you have
char *a_str = "a" and
char b_str[] = "b"
then &str is different things in the two cases -
&a_str is a pointer to a char *, so there actually has to be 4 bytes
in memory somewhere to point to the string.
&b_str is the same as b_str ... a pointer to the first character in
b_str.
(Similarly, sizeof(a_str) == 2, sizeof(b_str) == 4).
So, if they are global variables, then we need to reserve, in the first
case:
4 bytes for a_str (in a non-shared segment, because of the way shared
library relocations work. Unless you use prelink)
2 bytes for "a" (in a shared read-only segment)
In the second case we just need:
2 bytes for b_str/"b" (in a shared read-only segment
Isn't C wonderful?
Owen
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]