String constification

Francois Gouget fgouget at free.fr
Sat Apr 24 17:10:39 CDT 2004


On Sat, 24 Apr 2004, Dmitry Timoshkov wrote:

> "Francois Gouget" <fgouget at free.fr> wrote:
>
> > We find the same issues with an added twist: now that we can use
> > literals we can write things like:
> >
> >    const char* str = "String literal";
> >
> > However this is slightly different from:
> >
> >    static const char* str[] = "String literal";
> >
> > The difference is that in the first case sizeof(str) returns 4 (on 32
> > bit machines<g>) while in the latter we get 15. Some of our code relies
> > on this so care must be taken when converting the latter to the former.
>
> My understanding of this is that in the first case you declare a const
> pointer and hope that a compiler is smart enough to place a pointed by
> it object into a read only section, while in the second case you explicitly
> say to a compiler to place the whole object into a read only section.

In the first case you declare a const pointer and hope the compiler will
place the string in a const section. In the second case you declare a
const buffer and hope the compiler will put it in a read-only section.

If the compiler is too dumb to do the former I don't see that there's
any garantee it will do the latter. Also I'd say that if the compiler is
too dumb to handle one form or the other properly, then it's not worth
even considering. Fortunately it's a moot point because gcc handles both
cases just fine.


-- 
Francois Gouget         fgouget at free.fr        http://fgouget.free.fr/
                          "Utilisateur" (nom commun) :
       Mot utilis\xE9 par les informaticiens en lieu et place d'"idiot".



More information about the wine-devel mailing list