Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - reinterpret_cast problem

 
Thread Tools Search this Thread
Old 07-13-2004, 08:26 AM   #1
Default Re: reinterpret_cast problem


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
  Reply With Quote
Old 07-13-2004, 08:29 AM   #2
hack_tick
 
Posts: n/a
Default reinterpret_cast problem
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
  Reply With Quote
Old 07-13-2004, 08:30 AM   #3
Sharad Kala
 
Posts: n/a
Default Re: reinterpret_cast problem

"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
  Reply With Quote
Old 07-13-2004, 08:43 AM   #4
hack_tick
 
Posts: n/a
Default Re: reinterpret_cast problem
hi there
"John Harrison" <> 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





hack_tick
  Reply With Quote
Old 07-13-2004, 08:44 AM   #5
red floyd
 
Posts: n/a
Default Re: reinterpret_cast problem
hack_tick wrote:
> hi there
> "John Harrison" <> 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*


red floyd
  Reply With Quote
Old 07-13-2004, 09:04 AM   #6
hack_tick
 
Posts: n/a
Default Re: reinterpret_cast problem
hi there
"red floyd" <> wrote in message
news:ipMIc.10080$ 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




hack_tick
  Reply With Quote
Old 07-14-2004, 12:39 AM   #7
Karthik
 
Posts: n/a
Default Re: reinterpret_cast problem
hack_tick wrote:

> hi there
> "red floyd" <> wrote in message
> news:ipMIc.10080$ 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.


Karthik
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dial Up Problem smackedass A+ Certification 3 02-02-2007 11:59 PM
Re: Virus Problem ** Help!** David BlandIII A+ Certification 1 03-02-2004 06:00 PM
Pioneer DVR3100S problem with Satellite receiver Samsung DCR 9500 Fredrik Bengtsson DVD Video 0 12-12-2003 02:32 PM
Re: Serious Computer Problem hootnholler A+ Certification 1 11-24-2003 12:18 PM
Re: Serious Computer Problem Bret A+ Certification 0 11-19-2003 12:51 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46