[Bug c++/17507] New: Problems with shift and increment operator precedence
work at paul dot dubuc dot org
gcc-bugzilla@gcc.gnu.org
Wed Sep 15 18:42:00 GMT 2004
The following code is derived from an effecient string hashing function and
seems to indicate a GCC compiler bug. This code has been compiled with g++
2.95.3, 3.3.4 and 3.4.2 and behaves the same way in each case. But it behaves
differently (and the way we would expect) when compiled with the Sun Forte C++
compiler.
#include <iostream>
int
main(int argc, char** argv)
{
// Round address up to integral multiple
const char* pc = argv[1];
pc += 3;
pc = reinterpret_cast<const char*>(reinterpret_cast<unsigned>(pc) & ~3);
// Treat as pointer to unsigned, display address, and value
const unsigned int* pui = reinterpret_cast<const unsigned int*>(pc);
std::cout << " pui=" << (void*)pui << " *pui=" << std::hex << *pui
<< std::endl;
// Treat as pointer to unsigned char, display address
const unsigned char* puc = reinterpret_cast<const unsigned char*>(pc);
std::cout << "puc (before)=" << (void*)puc << std::endl;
// Obtain value by shift and OR
unsigned int uint = *puc++ << 24 | *puc++ << 16 | *puc++ << 8 | *puc++;
// Display address and value
std::cout << "puc (after)=" << (void*)puc << " uint=" << uint << std::endl;
return 0;
}
Output when compiled with Forte C++:
./testf abcdef
pui=ffbeea14 *pui=61626364
puc (before)=ffbeea14
puc (after)=ffbeea18 uint=61626364
Output when compiled with GCC:
./testg abcdef
pui=0xffbeea14 *pui=61626364
puc (before)=0xffbeea14
puc (after)=0xffbeea15 uint=61616161
The pointer value, puc, is only incremented once (instead of 4 times).
We are trying to work around this problem, but other implementations produce
slower code and speed is important here.
g++ -v
Reading specs from
/ldatae/gnu/gcc-3.3.4/bin/../lib/gcc-lib/sparc-sun-solaris2.8/3.3.4/specs
Configured with: ../gcc-3.3.4/configure
--prefix=/volws/pmd25/ldatae/gnu/gcc-3.3.4 --enable-shared --enable-threads
--enable-cpp --enable-languages=c++ --with-gnu-as
--with-as=/volws/pmd25/ldatae/gnu/gcc-3.3.4/bin/as --with-gnu-ld
--with-ld=/volws/pmd25/ldatae/gnu/gcc-3.3.4/bin/ld --host=sparc-sun-solaris2.8
Thread model: posix
gcc version 3.3.4
--
Summary: Problems with shift and increment operator precedence
Product: gcc
Version: 3.3.4
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: work at paul dot dubuc dot org
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: sparc-sun-solaris2.8
GCC host triplet: sparc-sun-solaris2.8
GCC target triplet: sparc-sun-solaris2.8
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17507
More information about the Gcc-bugs
mailing list