reinterpret_cast problem

Discussion in 'C++' started by John Harrison, Jul 13, 2004.

  1. On Tue, 13 Jul 2004 12:59:59 +0530, hack_tick <> wrote:

    > hi there
    > i m trying to convert (viod *) data to (unsigned char) using
    > reinterpret_cast as following
    >
    > void *aData; // some data already provided
    >
    > unsigned char c = reinterpret_cast<unsingned char>(aData);
    >
    > and the gcc is giving me the following error
    >
    > -------------------------------------------------
    > reinterpret_cast from
    > `void*' to `unsigned char' loses precision
    > -------------------------------------------------
    >
    > is this what i m doing is valid Standard C++ ???
    >
    > I have kept settings to treat all warning as Errors, so this is giving me
    > error
    >
    > any suggestions ??
    >


    Converting void* to unsigned char is a completely meaningless thing to do.
    As the warning states, void* is four bytes (probably) and unsigned char is
    one byte, so you are losing information.

    Are you sure you didn't mean to convert to unsigned char*? That would make
    a lot more sense.

    john
    John Harrison, Jul 13, 2004
    #1
    1. Advertising

  2. John Harrison

    hack_tick Guest

    hi there
    i m trying to convert (viod *) data to (unsigned char) using
    reinterpret_cast as following

    void *aData; // some data already provided

    unsigned char c = reinterpret_cast<unsingned char>(aData);

    and the gcc is giving me the following error

    -------------------------------------------------
    reinterpret_cast from
    `void*' to `unsigned char' loses precision
    -------------------------------------------------

    is this what i m doing is valid Standard C++ ???

    I have kept settings to treat all warning as Errors, so this is giving me
    error

    any suggestions ??
    hack_tick, Jul 13, 2004
    #2
    1. Advertising

  3. John Harrison

    Sharad Kala Guest

    "hack_tick" <> wrote in message
    news:...
    > hi there
    > i m trying to convert (viod *) data to (unsigned char) using
    > reinterpret_cast as following
    >
    > void *aData; // some data already provided
    >
    > unsigned char c = reinterpret_cast<unsingned char>(aData);
    >
    > and the gcc is giving me the following error


    reinterpret_cast means like telling the compiler that I know better than you
    here, just carry out what I am saying. That's unsafe and compiler warns you
    here that
    it could be that the destination type is not big enough to hold the source
    value. reinterpret_cast is very much standard C++.
    Sharad Kala, Jul 13, 2004
    #3
  4. John Harrison

    hack_tick Guest

    hi there
    "John Harrison" <> wrote in message
    news:eek:psa2ey3vg212331@andronicus...
    [..]
    > Converting void* to unsigned char is a completely meaningless thing to do.
    > As the warning states, void* is four bytes (probably) and unsigned char is
    > one byte, so you are losing information.
    >
    > Are you sure you didn't mean to convert to unsigned char*? That would make
    > a lot more sense.


    opps !! actually my mistake, it was (unsigned char *), sorry and thankx for
    making me realize that
    :)
    hack_tick, Jul 13, 2004
    #4
  5. John Harrison

    red floyd Guest

    hack_tick wrote:
    > hi there
    > "John Harrison" <> wrote in message
    > news:eek:psa2ey3vg212331@andronicus...
    > [..]
    >
    >>Converting void* to unsigned char is a completely meaningless thing to do.
    >>As the warning states, void* is four bytes (probably) and unsigned char is
    >>one byte, so you are losing information.
    >>
    >>Are you sure you didn't mean to convert to unsigned char*? That would make
    >>a lot more sense.

    >
    >
    > opps !! actually my mistake, it was (unsigned char *), sorry and thankx for
    > making me realize that
    > :)


    I believe that you can use static_cast<> to cast a void* into
    any-other-type*
    red floyd, Jul 13, 2004
    #5
  6. John Harrison

    hack_tick Guest

    hi there
    "red floyd" <> wrote in message
    news:ipMIc.10080$...
    [..]
    > I believe that you can use static_cast<> to cast a void* into
    > any-other-type*


    yup we can, i was doing all in hurry and forgt the actual data it was
    containing(address)

    thanks for your reply :)
    hack_tick, Jul 13, 2004
    #6
  7. John Harrison

    Karthik Guest

    hack_tick wrote:

    > hi there
    > "red floyd" <> wrote in message
    > news:ipMIc.10080$...
    > [..]
    >
    >>I believe that you can use static_cast<> to cast a void* into
    >>any-other-type*

    >
    >
    > yup we can, i was doing all in hurry and forgt the actual data it was
    > containing(address)
    >
    > thanks for your reply :)
    >
    >


    just to add , reinterpret cast is best when you try to convert an
    integer to a pointer. ( assuming the integer contains a valid address).
    It is no doubt dangerous, ( and not portable, to add ) , but assuming
    you know what you are doing , then it is best used in scenarios to
    convert an integer to a pointer.

    HTH

    - Karthik.
    Karthik, Jul 14, 2004
    #7
    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. Suzanne Vogel
    Replies:
    17
    Views:
    806
    Suzanne Vogel
    Jul 7, 2003
  2. Scott Brady Drummonds

    reinterpret_cast<>() v. static_cast<>()

    Scott Brady Drummonds, Jan 13, 2004, in forum: C++
    Replies:
    11
    Views:
    20,638
    Nick Hounsome
    Jan 20, 2004
  3. Aman

    reinterpret_cast<>

    Aman, Feb 23, 2004, in forum: C++
    Replies:
    15
    Views:
    843
    Ben Hutchings
    Feb 25, 2004
  4. johny smith

    const_cast, reinterpret_cast

    johny smith, Jun 24, 2004, in forum: C++
    Replies:
    18
    Views:
    3,438
    Old Wolf
    Jun 24, 2004
  5. Alex Vinokur
    Replies:
    1
    Views:
    574
Loading...

Share This Page