This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: correct transformations gcc 3.2
- From: Adrian Sheppard <aps110 at rsphysse dot anu dot edu dot au>
- To: Terrence <terrence at emc dot co dot za>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Tue, 8 Jul 2003 19:29:15 +1000 (EST)
- Subject: Re: correct transformations gcc 3.2
Terrence,
The problem is that "up" and "down" hold references to
temporary objects, namely the locales. The storage for these temporaries
is released immediately after construction of up and down, so up.loc and
down.loc end up pointing to memory that's waiting to be reused.
I was under the impression that gcc warned about references to
temporaries, but I must be wrong.
cheers
adrian
On Tue, 8 Jul 2003, Terrence wrote:
>Hi
>
>I've tried the following code on Redhat 8, gcc version 3.2. It compiles
>correctly but crashes when I run it. I got this code from
>http://linux-rep.fnal.gov/software/gcc/onlinedocs/libstdc++/22_locale/howto.html#7
>Why does it crash?
>
>Thanks in advance
>Terrence
>
>#include <iterator> // for back_inserter
> #include <locale>
> #include <string>
> #include <algorithm>
> #include <cctype> // old <ctype.h>
>
> struct Toupper
> {
> Toupper (std::locale const& l) : loc(l) {;}
> char operator() (char c) { return std::toupper(c,loc); }
> private:
> std::locale const& loc;
> };
>
> struct Tolower
> {
> Tolower (std::locale const& l) : loc(l) {;}
> char operator() (char c) { return std::tolower(c,loc); }
> private:
> std::locale const& loc;
> };
>
> int main ()
> {
> std::string s ("Some Kind Of Initial Input Goes Here");
> Toupper up ( std::locale("C") );
> Tolower down ( std::locale("C") );
>
> // Change everything into upper case
> std::transform (s.begin(), s.end(), s.begin(),
> up
> );
>
> // Change everything into lower case
> std::transform (s.begin(), s.end(), s.begin(),
> down
> );
>
> // Change everything back into upper case, but store the
> // result in a different string
> std::string capital_s;
> std::transform (s.begin(), s.end(), std::back_inserter(capital_s),
> up
> );
> }
>