This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC 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]

other/4928: (unsigned long long) cast with random top 4 bytes

[Get raw message]

>Number:         4928
>Category:       other
>Synopsis:       (unsigned long long) cast with random top 4 bytes
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 22 07:06:01 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Aaron Naiman
>Release:        egcs-2.91.66
>Organization:
>Environment:
Linux (egcs-1.1.2 release)
>Description:
For the following code:

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
#include <iostream.h>
#include <limits.h>

int main (void) {
    int i;
  
    unsigned addr = (unsigned) &i;
    unsigned long long addr1 = (unsigned long long) addr,
                       addr2 = (unsigned long long) &i,
                       addr3 = addr2 % UINT_MAX;

    cout << (unsigned) &i << endl
         << addr1         << endl
         << addr2         << endl
         << addr3         << endl;

    return 0;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I get:

vvvvvvvvvvvvvvvvvvvvvv
3221223220
3221223220
18446744072635807540
3221223220
^^^^^^^^^^^^^^^^^^^^^^

so the cast is not happening properly (all "on" bits in the
first four bytes?).  If I write this in C, I get the same
results, but (with -Wall) there is a warning that for the
definition of addr2:

warning: cast from pointer to integer of different size

Qs: a) What is the calculation that is going on?
    b) What is exactly wrong, that this warning is
       reporting?
    c) Why is there no warning in g++?

adTHANXvance!

Aaron
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


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