473,698 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reinterpret_cas t problem

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

void *aData; // some data already provided

unsigned char c = reinterpret_cas t<unsingned char>(aData);

and the gcc is giving me the following error

-------------------------------------------------
reinterpret_cas t 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 ??
Jul 22 '05 #1
6 2791
On Tue, 13 Jul 2004 12:59:59 +0530, hack_tick <ha*******@yaho o.com> wrote:
hi there
i m trying to convert (viod *) data to (unsigned char) using
reinterpret_cas t as following

void *aData; // some data already provided

unsigned char c = reinterpret_cas t<unsingned char>(aData);

and the gcc is giving me the following error

-------------------------------------------------
reinterpret_cas t 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
Jul 22 '05 #2

"hack_tick" <ha*******@yaho o.com> wrote in message
news:2l******** ****@uni-berlin.de...
hi there
i m trying to convert (viod *) data to (unsigned char) using
reinterpret_cas t as following

void *aData; // some data already provided

unsigned char c = reinterpret_cas t<unsingned char>(aData);

and the gcc is giving me the following error


reinterpret_cas t 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_cas t is very much standard C++.

Jul 22 '05 #3
hi there
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsa2ey3vg 212331@andronic us...
[..]
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
:-)
Jul 22 '05 #4
hack_tick wrote:
hi there
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsa2ey3vg 212331@andronic us...
[..]
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*
Jul 22 '05 #5
hi there
"red floyd" <no*****@here.d ude> wrote in message
news:ip******** *********@newss vr25.news.prodi gy.com...
[..]
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(addr ess)

thanks for your reply :-)
Jul 22 '05 #6
hack_tick wrote:
hi there
"red floyd" <no*****@here.d ude> wrote in message
news:ip******** *********@newss vr25.news.prodi gy.com...
[..]
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(addr ess)

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.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
12826
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. To get around this limitation on double, I'd like to keep the bits of the double the *same* but change its interpretation to long. I can use "&" on longs. I tried to use reinterpret_cast for this purpose, but it returned zero every time. double...
2
2434
by: Taran | last post by:
Hi All, I was trying some code which essentially does what the function 'func' here does. To simplify the things and make a consise post I have created a dummy app to show my problem. The issue was to assign a value to an array of pointers. The selection of array depends upon a condition and the void* ptr has to be casted to the correct type and inserted in the array.
7
10904
by: Peter | last post by:
I never used reinterpret_cast -- probably because I don't know what it means. Can somebody enlighten me? I looked into Stroustrup's "The annoted C++ reference manual" -- but this was no help. Can I assume that reinterpret_cast is not safe and should not be used? Does it always succeed even if the cast is garbage?
4
2664
by: tkirankumar | last post by:
Hi all, This is regarding the issue I am facing while porting the my application from SuSe Linux to Sun Solaris. The Existing system details: Itanium boxes 1mhz 4 processor 8 gb machines with SuSe Linux $ uname -a Linux longrtedged01 2.4.21-215-itanium2-smp #1 SMP Mon Apr 26 16:28:29
27
4335
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong type was the target of a cast. Of course with a reinterpret_cast nothing complains until the UB bites you in the ass. It seems to me that there ought to be a way to deal with these kinds of functions yet still retain some semblance of type...
10
3809
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my application that the code be speed optimized ) I use reinterpret_cast to alias a block of chars read in from disc as a block of integer "words". My existing code (see simplified code below) appears to work well enough on the platforms available to me,...
2
2955
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
2
2677
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when optimizing. #include <iostream> int main() { long D1 = 0xbcfbc4f0d9b65179; long D2 = 0xbcfbc4f0d9b65042;
6
2412
by: Husyn Raj | last post by:
class person { protected: char name; int age; public: void getdata() {cout<<"Enter name"; cin>>name; cout<<"Enter age"; cin>>age;
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.