This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Making strings of arbitrary character types
On Wed, 2004-12-01 at 11:22 +0100, Paolo Carlini wrote:
> Right, 3.4.x is vastly better in this area (and we don't have any open
> bug ;)
I hope this isn't the first. Compiling the program below w/ GCC 3.4.2
and running produces:
terminate called after throwing an instance of 'std::bad_cast'
what(): St8bad_cast
Aborted
--- CODE BEGIN ---
#include <string>
#include <iostream>
#include <fstream>
typedef unsigned char UINT8;
typedef std::basic_string<UINT8> ByteArray;
void writeBytes()
{
ByteArray bytes(30, (UINT8)'A');
std::basic_ofstream<ByteArray::value_type> fout("test.dat",
std::ios::binary | std::ios::out);
fout << bytes; // Exception thrown here
}
int main(int argc, char *argv[])
{
writeBytes();
return(0);
}
--- CODE END ---
Basically, the exception is thrown because the locale is not set.
Backtrace:
#0 std::__check_facet<std::codecvt<unsigned char, char, __mbstate_t> >
(__f=0x0) at localefwd.h:187
#1 0x0804cfe3 in std::basic_filebuf<unsigned char,
std::char_traits<unsigned char> >::xsputn (this=0xbfffef74,
__s=0x805005c 'A' <repeats 30 times>, __n=30) at fstream.tcc:507
#2 0x0804ac5e in std::basic_streambuf<unsigned char,
std::char_traits<unsigned char> >::sputn (this=0xbfffef74, __s=0x805005c
'A' <repeats 30 times>, __n=30) at streambuf:421
#3 0x0804a2af in std::basic_ostream<unsigned char,
std::char_traits<unsigned char> >::_M_write (this=0xbfffef70,
__s=0x805005c 'A' <repeats 30 times>, __n=30) at ostream:288
#4 0x08049867 in std::operator<< <unsigned char,
std::char_traits<unsigned char>, std::allocator<unsigned char> >
(__out=@0xbfffef70, __str=@0xbffff0a0) at ostream.tcc:665
#5 0x08049276 in writeBytes () at main.cpp:13
#6 0x0804930b in main (argc=1, argv=0xbffff134) at main.cpp:18
GCC -v:
Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.2/specs
Configured with: /var/tmp/portage/gcc-3.4.2/work/gcc-3.4.2/configure
--enable-version-specific-runtime-libs --prefix=/usr
--bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4
--includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.2/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4/info
--with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.2/include/g
++-v3 --with-local-prefix=/usr/local --host=i686-pc-linux-gnu
--disable-multilib --enable-nls --without-included-gettext
--enable-__cxa_atexit --enable-clocale=gnu --enable-shared
--with-system-zlib --disable-checking --disable-werror
--disable-libunwind-exceptions --with-gnu-ld --enable-threads=posix
--enable-languages=c,c++ --disable-libgcj
Thread model: posix
gcc version 3.4.2
--
Regards,
Nick G.
http://www.wheeloyum.com