CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2005
    Posts
    6

    Question reinterpret_cast

    Can anyone look at it and help me figure out how a reinterpret cast works?

    Here is the code :

    main()
    {

    int * ptr;
    int i = 20;
    ptr = &i;
    cout<<ptr<<endl;
    i = reinterpret_cast<int>(ptr);
    cout<<i<<endl;

    int * ptr1;
    int i1 = 200;
    ptr1 = &i1;
    cout<<ptr1<<endl;
    i= reinterpret_cast<int>(ptr1);
    cout<<i1<<endl;

    char* a = " This is a string";
    i = reinterpret_cast<int>(a);
    cout<<&a[0]<<endl<<i<<endl;

    char* a1 = "This is something else";
    i1 = reinterpret_cast<int>(a);
    cout<<&a1[0]<<endl<<i1<<endl;



    }


    and here is the o/p:


    0012FF78
    1245048
    0012FF70
    200
    This is a string
    4636728
    This is something else
    4636728
    Press any key to continue


    How does it work ??
    Whats happenin?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: reinterpret_cast

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: reinterpret_cast

    Code:
    i= reinterpret_cast<int>(ptr1);
    cout<<i1<<endl;
    I think you have a typo in these two lines. Didn't you mean to cout "i", not i1?

    Basically, reinterpret_cast takes whatever you put between the "()" and treats it as if it were of the type specified between the "<>".

    Don't be confused, BTW, that outputting a pointer generates hex, whereas outputting an integer generates decimal.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: reinterpret_cast

    There are basically two things to understand in your program:

    • reinterpret_cast works on pointers or integers of the same size (for example 4 bytes), and do no special treating on them. So, it does not really convert a value to another, but interprets a value as a value of another type.
    • cout (which is an instance of basic_ostream<char> class) overloads operator>> for different types.
      And each type has a different interpretation and representation by this operator.


    With cout, integers are displayed in decimal format (this behavior can be modified, using a manipulator or setting a bit mask).
    With cout, pointers other than const char* are outputed as the address contained in the pointer in hexadecimal format.
    And, finally, there is an overload for const char* which outputs each character pointed by the pointer and above the address pointed by the pointer, until a '\0' character is found.

    0012FF78 is the address of i displayed in hexadecimal format.
    1245048 is the same address, but displayed in decimal format (you can verify with calc.exe).
    0012FF70 is the address of i1 in hexadecimal format.

    4636728 is the address of the first string, displayed in decimal format.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured