Basic question about casting and addresses

Discussion in 'C++' started by drowned, Aug 2, 2003.

  1. drowned

    drowned Guest

    I'm having a problem understanding why the code in this little program
    I've got is behaving the way it does... if anyone can explain it, I'd
    be grateful:


    #include <iostream>
    using namespace std;

    //This function takes an unsigned char (one byte on my machine)
    //and prints it in binary notation
    void printBinary(const unsigned char val) {
    for(int i = 7; i >= 0; i--)
    if(val & (1 << i))
    std::cout << "1";
    else
    std::cout << "0";
    }


    int main() {
    float f=1;
    unsigned char uc = (unsigned char) &f;
    unsigned char* ucp = &uc;
    for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
    cout << "ucp[" << i << "] = " << (long)ucp << endl;
    cout << "ucp[" << i << "] = "; printBinary(ucp); cout << endl;
    }
    }



    If I understand what is going on here correctly, I am taking the
    address of the float and casting it to an unsigned char. I then assign
    that value to the unsigned char pointer. When I change the value of
    the float, however, ucp's value changes. I would think that ucp would
    not change because it holds an address... what does changing the value
    of the float have to do with its address?
     
    drowned, Aug 2, 2003
    #1
    1. Advertising

  2. drowned wrote:
    > I'm having a problem understanding why the code in this little program
    > I've got is behaving the way it does... if anyone can explain it, I'd
    > be grateful:
    >
    >
    > #include <iostream>
    > using namespace std;
    >
    > //This function takes an unsigned char (one byte on my machine)
    > //and prints it in binary notation
    > void printBinary(const unsigned char val) {
    > for(int i = 7; i >= 0; i--)
    > if(val & (1 << i))
    > std::cout << "1";
    > else
    > std::cout << "0";
    > }
    >
    >
    > int main() {
    > float f=1;
    > unsigned char uc = (unsigned char) &f;
    > unsigned char* ucp = &uc;
    > for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
    > cout << "ucp[" << i << "] = " << (long)ucp << endl;
    > cout << "ucp[" << i << "] = "; printBinary(ucp); cout << endl;
    > }
    > }
    >


    Let's see how memory *might* be layed out:

    stack_frame : <return address 0>
    stack_frame : <return address 1>
    stack_frame : <return address 2>
    stack_frame : <return address 3>
    f : <float byte 0>
    f : <float byte 1> <<<<<<<<<< 3 + &uc
    f : <float byte 2> <<<<<<<<<< 2 + &uc
    f : <float byte 3> <<<<<<<<<< 1 + &uc
    uc : <char byte 0> <<<<<<<<<< &uc
    filler : <random junk>
    filler : <random junk>
    filler : <random junk>
    ucp : <unsigned char* byte 0>
    ucp : <unsigned char* byte 1>
    ucp : <unsigned char* byte 2>
    ucp : <unsigned char* byte 3>

    &uc + 3 bytes is within the float.

    >
    >
    > If I understand what is going on here correctly, I am taking the
    > address of the float and casting it to an unsigned char. I then assign
    > that value to the unsigned char pointer. When I change the value of
    > the float, however, ucp's value changes. I would think that ucp would
    > not change because it holds an address... what does changing the value
    > of the float have to do with its address?


    What your doing is undefined because you take the address of an object
    of size 1 and then reference 3 more objects after that. Who knows what
    the compiler should or would or has done or not done. What I
    demonstrated above is what *SOME* compilers do but it's not even correct
    because some compilers *may* not decide to layout memory in this way.

    In other words - DON'T do it.

    G
     
    Gianni Mariani, Aug 2, 2003
    #2
    1. Advertising

  3. drowned wrote:
    > I'm having a problem understanding why the code in this little program
    > I've got is behaving the way it does... if anyone can explain it, I'd
    > be grateful:
    >
    >
    > #include <iostream>
    > using namespace std;
    >
    > //This function takes an unsigned char (one byte on my machine)


    A char is a byte on all machines. This is required by the C++ standard.

    > //and prints it in binary notation
    > void printBinary(const unsigned char val) {
    > for(int i = 7; i >= 0; i--)
    > if(val & (1 << i))
    > std::cout << "1";
    > else
    > std::cout << "0";
    > }
    >
    >
    > int main() {
    > float f=1;
    > unsigned char uc = (unsigned char) &f;


    This is not a very meaningful thing to do. The result is very unlikely
    to be the address of f, or anything useful for that matter. Also, using
    C-style casts is not recommended in most cases. Use C++ cast operators
    instead.

    > unsigned char* ucp = &uc;
    > for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
    > cout << "ucp[" << i << "] = " << (long)ucp << endl;
    > cout << "ucp[" << i << "] = "; printBinary(ucp); cout << endl;
    > }
    > }
    >
    >
    >
    > If I understand what is going on here correctly, I am taking the
    > address of the float and casting it to an unsigned char. I then assign
    > that value to the unsigned char pointer.


    No. You assign the address of your unsigned char (uc) to your unsigned
    char pointer (ucp).

    > When I change the value of
    > the float, however, ucp's value changes.


    I don't know of any reason this would happen, and I see no evidence for
    it in the code you posted. Post code demonstrating this if you'd like it
    explained.

    > I would think that ucp would
    > not change because it holds an address... what does changing the value
    > of the float have to do with its address?


    Nothing. There does not appear to be any relationship at all between the
    float f and the unsigned char * ucp.

    -Kevin
     
    Kevin Goodsell, Aug 2, 2003
    #3
  4. "drowned" <> wrote in message
    news:...
    ....
    > int main() {
    > float f=1;
    > unsigned char uc = (unsigned char) &f;
    > unsigned char* ucp = &uc;
    > for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
    > cout << "ucp[" << i << "] = " << (long)ucp << endl;
    > cout << "ucp[" << i << "] = "; printBinary(ucp); cout << endl;
    > }
    > }

    You probably mean

    unsigned char* ucp = (unsigned char *)&f;

    --
    Roger
     
    Roger Willcocks, Aug 3, 2003
    #4
  5. drowned

    Rolf Magnus Guest

    drowned wrote:

    > I'm having a problem understanding why the code in this little program
    > I've got is behaving the way it does... if anyone can explain it, I'd
    > be grateful:
    >
    >
    > #include <iostream>
    > using namespace std;
    >
    > //This function takes an unsigned char (one byte on my machine)


    An unsigned char _always_ is one byte. Note however that a byte may be
    bigger than 8bits.

    > //and prints it in binary notation
    > void printBinary(const unsigned char val) {
    > for(int i = 7; i >= 0; i--)
    > if(val & (1 << i))
    > std::cout << "1";
    > else
    > std::cout << "0";
    > }
    >
    >
    > int main() {
    > float f=1;
    > unsigned char uc = (unsigned char) &f;


    Here, you convert the address of f into an unsigned char. Of course
    parts of that address will probably be lost, since on most machines,
    pointers are bigger than unsigned chars.

    > unsigned char* ucp = &uc;


    Here, you create a pointer that points to the converted address (or what
    is left from it), which is one byte big. I'm quite sure that you wanted
    instead to convert the address of the float into a pointer to unsigned
    char, for which you'd have to replace your above two lines with:

    unsigned char* ucp = reinterpret_cast<unsigned char*>(&f);

    > for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
    > cout << "ucp[" << i << "] = " << (long)ucp << endl;
    > cout << "ucp[" << i << "] = "; printBinary(ucp); cout << endl;
    > }


    In that loop, you try to read 4 bytes through the above pointer, which
    only points to one single unsigned char.

    > }
    >
    >
    >
    > If I understand what is going on here correctly, I am taking the
    > address of the float and casting it to an unsigned char.


    Yes.

    > I then assign that value to the unsigned char pointer.


    No, you assign the address of the resulting unsigned char to the
    unsigned char pointer.

    > When I change the value of
    > the float, however, ucp's value changes. I would think that ucp would
    > not change because it holds an address...


    How do you change it?

    > what does changing the value of the float have to do with its address?


    Nothing, I'd say.
     
    Rolf Magnus, Aug 3, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,842
    Andrew McDonagh
    Jan 8, 2005
  2. namespace1
    Replies:
    3
    Views:
    950
  3. Wally Barnes
    Replies:
    3
    Views:
    545
    Wally Barnes
    Nov 20, 2008
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    596
    James Kanze
    Dec 20, 2009
  5. Piotrne

    A basic (?) problem with addresses (gcc)

    Piotrne, Dec 15, 2010, in forum: C Programming
    Replies:
    44
    Views:
    1,195
    Tim Rentsch
    Jan 10, 2011
Loading...

Share This Page