help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: locales and stl


From: Roger Leigh
Subject: Re: locales and stl
Date: Sun, 30 Apr 2006 13:48:50 +0100
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dubourg@gmail.com writes:

> I am currently struggling with what appears to be a locale issue with
> gcc 3.3.5 on Red Hat Enterprise Linux AS release 3 (Taroon) (kernel
> 2.4.21-4.ELsmp). Specifically, when I compile my project with LANG and
> LANGVAR environmental variables set to en_US.UTF-8, the executable
> behaves correctly. However when unset those variables, compile and run
> the project, it segfaults in the first call to the [] operator of an
> initialized std::map<std::string, Foo *>. I've checked what the default
> locale is in that case via cout.getLoc().name() and it reports "C"
>
> I can't post my project, so I understand that you can't provide
> specific answers. However, from what I've described, can anyone give me
> some general suggestions and/or point me to a resource where I can
> learn more about the inner workings of g++ regarding locales?

LANGVAR is not a standard environment variable.  You can probably
ignore it.  LANG is standard, however.

Try running the program with

LANG=C ./myprog
LANG=en_US.UTF-8 ./myprog

and check for differences.  You might be exposing a bug in your map
usage.  Replace map[] with map.find(string), and check if the returned
iterator is valid (remember than [] can't cope with failure).  This is
the most likely cause of the segfault.  Rule of thumb: never use [] in
all but trivial test code, since you can't cope with failure.

If this doesn't work, reduce the failure to a minimal testcase, and if
you still can't find the cause of failure, post that minimal testcase
here.

As far as locale goes, remember to set up the locale stuff properly
when the program starts up, otherwise it will default to the "C"
locale:

      // Set up locale.
      std::locale::global(std::locale(""));
      std::cout.imbue(std::locale());
      std::cerr.imbue(std::locale());

Another point to note: GCC 3.3 and above (and probably earlier 3.x
releases) use UTF-8 as the execution character set, and they also use
UTF-8 as the default input charset (for your source code).  All the
string literals are UTF-8, and wide strings are UTF-32.  This should
be the case irrespective of the locale you invoke the compiler under,
but if you run under a non-UTF-8 locale, the input charset may
possibly be different (though I doubt it--it should autodetect UTF-8
source).  You can use various -f options to change the defaults, but I
advise against it.  This is all in the GCC Info manual.

GCC's C++ locale support is much improved in more recent compilers.
Give 3.4 or 4.0 (or 4.1) a try.


Regards,
Roger
- -- 
Roger Leigh
                Printing on GNU/Linux?  http://gutenprint.sourceforge.net/
                Debian GNU/Linux        http://www.debian.org/
                GPG Public Key: 0x25BFB848.  Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFEVLIwVcFcaSW/uEgRAv2QAJ4t4hEN72KQ1DzCZdnK2875XIGSFwCdHTGy
kciPwAGPcJm9HMmCG2ZGj9w=
=z+6K
-----END PGP SIGNATURE-----


reply via email to

[Prev in Thread] Current Thread [Next in Thread]