Thread: reintepret_cast

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    reintepret_cast

    hey guys,
    i was doing this examples given on the book when i encountered this problem on reintepet_cast although i guess its not on that topic.. help me

    Code:
    #include <iostream.h>
    
    #include <stdlib.h>
    
    using namespace std;
    
    const int sz = 10;
    
    struct X { int a[sz]; };
    
    void print(X* x) {
    
      for(int i = 0; i < sz; i++)
    
        cout << x->a[i] << ' ';
    
      cout << endl << "--------------------" << endl;
    
    }
    
    int main() {
    
      X x;
    
      print(&x);
    
      int* xp = reinterpret_cast<int*>(&x);
    
      for(int* i = xp; i < xp + sz ; i++) 
    // hey why is this  xp  + sz here ???
    // i didnt get this.
    //rest is fine with me ;)
    
        *i = 0;
      
    print(reinterpret_cast<X*>(xp));
      
    system("pause");
    
    }
    Code tags added by Hammer
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: reintepret_cast

    Its a pretty scuzzy way of accessing structure contents...

    int* xp = reinterpret_cast<int*>(&x);

    Take the address of the start of the struct (which is filled with ints in an array) and cast it to an int pointer......reasoning being that if you deference this pointer it will give you the first int in the array.

    for(int* i = xp; i < xp + sz ; i++)

    Now iterate up the memory by the size of an int each time (basically walk the array of ints)...

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    Smile but hey!

    why this xp+xz ???
    i got the rest of the part.. iterating and all that.thanx alot.
    for(int* i = xp; i < xp + sz ; i++)
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    xp is the first in in the array.....and the array is sz ints long....so as i starts at xp, it can be incremented sz times before the i < xp + sz test returns false

  5. #5
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    some confusion still!

    doesnt this statement

    int* xp = reinterpret_cast<int*>(&x);

    makes xp = address of x ???

    for(int* i = xp; i < xp + sz ; i++)

    well here in the loop xp's value is in hex and sz is in decimal. how

    can they be resolved??

    i am just a beginner so be patient with me
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ahhh..

    i mean how can they be added hex + decimal??
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: ahhh..

    Originally posted by newbie_grg
    i mean how can they be added hex + decimal??
    Its not Hex + Decimal...its memory and offset

    You do that every time you use an array

    int x[10];

    //------ Fill array

    int y = x [3];//x is a memory location and you add 3 * sizeof(int).

    so xp + sz is sz ints after xp...which takes it after the end of the array, so as sson as you are about to access the variable after the array it stops.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The computer can add decimal and hex together because everything is actually stored in binary. The decimal or hex format is a conversion for input / output.

    You could add a decimal number to a hex number, right? I could say add 10 decimal to A hex and give me the answer in decimal... then give me the answer in hex.

Popular pages Recent additions subscribe to a feed