This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

iostream changes locale to format a number


Some calls to iostreams seem to alter locale by calling setlocale
system call inside. Is this a transitional problem which is going
to be fixed when newer glibc is released, or to be fixed independently
in libstdc++?

* problem
Using the followings,
kernel: linux 2.4.18
glibc: 2.2.5 (glibc-2.2.5-32.i686.rpm)
gcc: gcc3 (gcc3-3.1-0.23.1.rpm)

the following source code

--- Source begins ---
#include<clocale>
#include<iostream>

using namespace std;

int main(int argc, char* argv[])
{
  cout << setlocale(LC_ALL, "") << endl;
  cout << setlocale(LC_ALL, NULL) << endl;
  cout << "Hello, World!" << endl;
  cout << setlocale(LC_ALL, NULL) << endl;
  cout << 1 << endl;
  cout << setlocale(LC_ALL, NULL) << endl;
  exit(0);
}
--- Source ends ---

leads to

--- Execution begins ---
ja_JP.eucJP
ja_JP.eucJP
Hello, World!
ja_JP.eucJP
1
C
--- Execution ends ---

executing under LANG=ja_JP.eucJP.
I guess other locales than "C" also leads to the similar problem.

* a quick solution
According to the manpage setlocale(3),
setlocale returns newlly set locale instead of previous one
when called with locale string in the second argument.
In order to get previous locale, one seems to call setlocale with NULL
in the second argument.
So, a quick solution is to change source in libstdc++-v3 like,

--- Original Source begins ---
      const char* __old = setlocale(LC_ALL, "C");
      ...
      setlocale(LC_ALL, __old);
--- Original Source ends ---

--- Modified Source begins ---
      const char* __old = setlocale(LC_ALL, NULL);
      setlocale(LC_ALL, "C");
      ...
      setlocale(LC_ALL, __old);
--- Modified Source ends ---

* Relations to other issue

Related problem has been once reported in gcc-list
from the point of view of MT-save in

http://gcc.gnu.org/ml/gcc/2002-03/msg00788.html
http://gcc.gnu.org/ml/libstdc++/2002-03/msg00396.html

---
	Takeshi Kobayakawa
	tskoba@mte.biglobe.ne.jp


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]