Alignment problem in std::basic_string
James KANZE
jkanze@caicheuvreux.com
Thu Nov 21 06:11:00 GMT 2002
The appended program core dumps with a bus error when compiled with -O3
on a Sparc.
Formally, the code is undefined behavior, because the standard doesn't
require an implementation to provide anything in the line of
char_traits<double>. Implementing my own char_traits in order to be
100% would add a lot of code to the example, without changing anything
fundamental.
I've looked at the source code, and there is definitly an alignment
problem in the implementation of std::basic_string.
Compiler version: g++3 (GCC) 3.1
OS version: Solaris 2.8
Machine: Sun UltraSparc 5/10
Program:
-------------------------------------------------------
#include <string>
#include <iostream>
#include <ostream>
int
main()
{
typedef std::basic_string< double >
dstring ;
static double const init[] = { 1.5, 3.14, 10 } ;
dstring d( init, init + 3 ) ;
for ( dstring::size_type i = 0 ; i < 3 ; ++ i ) {
std::cout << &d.at( i ) << ": " << d.at( i ) << '\n' ;
}
return 0 ;
}
-------------------------------------------------------
Compiled with: g++ -O3 -o dstring dstring.cc
Invoked with: dstring
The code works (!) if compiled without optimization. This is apparently
due to the non-optimized version using two instructions to load and
store floating point values, instead of only one as in the optimized
version.
--
James Kanze mailto:jkanze@caicheuvreux.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
"This message, including any attachments may contain confidential and
privileged material; it is intended only for the person to whom it is
addressed. Its contents do not constitute a commitment by Crédit
Agricole Indosuez Cheuvreux except where provided for in a written
agreement. Crédit Agricole Indosuez Cheuvreux assumes no liability or
responsibility for the consequences arising out of a delay and/or loss
in transit of this message, or for corruption or other error(s)
arising in its transmission and for any misuse or fraudulent use which
may be made thereof. If you are not the intended recipient, please
contact us and abstain from any disclosure, use or dissemination. To
the extent that this message contains research information and/or
recommendations, these are provided on the same basis as Crédit
Agricole Indosuez Cheuvreux's published research and the recipient
must have regard to all disclosures and disclaimers contained
therein."
More information about the Gcc-bugs
mailing list