A 32 bit pointer with highest bit set ( >= 0x80000000) is incorrectly cast to unsigned long long. The top 32 bits of the unsigned long long are all set 1, not 0. The problem does not occur if the pointer is less than 0x80000000 or if it is first cast to unsigned long and that is then cast to unsigned long long.
Obviously the highest bit would indicate a negative value in a signed int of the same size. It looks like some signed logic is being invoked inappropriately Details provided from 4.3.3 on Ubuntu, also seen on 4.4.1 on Ubuntu and 4.1.2 on NetBSD. Exact same behaviour also seen with Microsoft Visual C++ 2008, but not on Apple's version of 4.0.1. Here is a small test program which demonstrates the problem, I'll attach the .ii and gcc output: #include <iostream> #include <iomanip> #include <string> using namespace std; template<typename T> void dump(string legend, T* data) { // Display the bytes of the supplied type and its cout operator << rendition unsigned char* puc = (unsigned char*)data; cout << legend << ":" << endl; for(unsigned int i = 0; i < sizeof(T); i++) cout << hex << setw(2) << setfill('0') << (unsigned int)(puc[i]) << " "; cout << endl << hex << setw(16) << setfill('0') << *data << endl << endl; } int main(int argc, char **argv) { void* pv1 = (void*)0x12345678; void* pv2 = (void*)0x87654321; unsigned long long int ulli; dump("small void*", &pv1); ulli = (unsigned long long int)pv1; dump("small void* cast to unsigned long long", &ulli); ulli = (unsigned long long int)(unsigned long int)pv1; dump("small void* cast to unsigned long cast to unsigned long long", &ulli); dump("large void*", &pv2); ulli = (unsigned long long int)pv2; dump("large void* cast to unsigned long long", &ulli); ulli = (unsigned long long int)(unsigned long int)pv2; dump("large void* cast to unsigned long cast to unsigned long long", &ulli); } -- Summary: 32 bit pointer in top half of VM casts to unsigned long long incorrectly Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: alangcarter at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43645