Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reinterpret_cast problem

Reply
Thread Tools

reinterpret_cast problem

 
 
John Harrison
Guest
Posts: n/a
 
      07-13-2004
On Tue, 13 Jul 2004 12:59:59 +0530, hack_tick <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
hack_tick
Guest
Posts: n/a
 
      07-13-2004
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 ??


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      07-13-2004

"hack_tick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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++.



 
Reply With Quote
 
hack_tick
Guest
Posts: n/a
 
      07-13-2004
hi there
"John Harrison" <(E-Mail Removed)> wrote in message
newspsa2ey3vg212331@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



 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-13-2004
hack_tick wrote:
> hi there
> "John Harrison" <(E-Mail Removed)> wrote in message
> newspsa2ey3vg212331@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*
 
Reply With Quote
 
hack_tick
Guest
Posts: n/a
 
      07-13-2004
hi there
"red floyd" <(E-Mail Removed)> wrote in message
news:ipMIc.10080$(E-Mail Removed) m...
[..]
> 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


 
Reply With Quote
 
Karthik
Guest
Posts: n/a
 
      07-13-2004
hack_tick wrote:

> hi there
> "red floyd" <(E-Mail Removed)> wrote in message
> news:ipMIc.10080$(E-Mail Removed) m...
> [..]
>
>>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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
const_cast, reinterpret_cast johny smith C++ 18 06-24-2004 09:53 PM
reinterpret_cast<> Aman C++ 15 02-25-2004 03:03 PM
reinterpret_cast<>() v. static_cast<>() Scott Brady Drummonds C++ 11 01-20-2004 09:12 PM
reinterpret_cast - to interpret double as long Suzanne Vogel C++ 17 07-07-2003 02:50 PM



Advertisments