-
July 14th, 2006, 01:28 PM
#1
reinterpret_cast question
Hi,
I have been reading in a lot of places that using reinterpret_cast is bad but they never explain the reason for it. I use it a lot in my code to convert int to char * since I need to send the individual bytes of the int across a network.
<CODE>
char *byteCrcPtr = NULL;
DWORD byteCrc = 0xFF23F2e4;
byteCrcPtr = reinterpret_cast<char *>(&byteCrc);
for(int i=0;i<sizeof(DWORD);i++) {
packet[i]= byteCrcPtr[i];
}
</CODE>
To convert back
<CODE>
DWORD byteCrc = *(reinterpret_cast<DWORD*>(&packet[9]));
</CODE>
I especially want to understand why this is bad and is there a better way to do this. Thanks
Amish
-
July 14th, 2006, 02:19 PM
#2
Re: reinterpret_cast question
Recall how guns can be useful and can be mis-used as well . The language features that are misused most of the times are sometimes suggested as dangerous to use .. but sometimes their need is reasonable and that explains their presence.
The basic idea is type-safety. reinterpret cast (i will call it RC henceforth) is capable of casting any type to any other type and back successfully. Whenever, you are designing an application you would not want to play with types in that sense, else one single variant type should have had been sufficient in any programming language, though I cannot bet.
C++ is a strongly typed language. And one's design should respect that otherwise it will lead to a messed up application with hard-to-spot-errors. This has been proven time and again. You lose the type information and for some other casting operators, you get an overhead.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 14th, 2006, 02:21 PM
#3
Re: reinterpret_cast question
reinterpret_cast is used to cast unrelated types such as a pointer to an integer. I believe that the use of reinterpret_cast is not guaranteed to be portable. Furthermore, using reinterpret_cast is very heavy-handed and might mask problematic design issues.
Many developers use a combination of two static_cast's to cast pointer to integral types.
Sincerely, Chris.
You're gonna go blind staring into that box all day.
-
July 14th, 2006, 05:05 PM
#4
Re: reinterpret_cast question
Thanks for the reply. Exterminator could you explain more about what you mean by "You lose the type information and for some other casting operators, you get an overhead."
Also any idea how to cast an integer into a char pointer. The problem is that I need to create a packet containing the individual bytes that make up the integer. It is to be used as a packet in a network application.
I'll also look into static cast. Thanks
Amish
Last edited by axr0284; July 14th, 2006 at 05:08 PM.
-
July 14th, 2006, 05:31 PM
#5
Re: reinterpret_cast question
Originally Posted by axr0284
I have been reading in a lot of places that using reinterpret_cast is bad but they never explain the reason for it. I use it a lot in my code to convert int to char * since I need to send the individual bytes of the int across a network.
reinterpret_cast is very non-portable and leads easily to undefined behavior.
During a long time I even thought that you couldn't reinterpret_cast a pointer to another pointer to char and use this pointer to char, though, now that I have read [expr.reinterpret_cast]-10 in the ISO 14882:1998 standard, I think that it can be portable.
Anyway, reinterpret_cast is very dangerous : You must keep constantly in mind things like data alignment and the original type of the pointer.
http://www.codeguru.com/forum/showpost.php?p=1385744
Furthermore, even if this use of reinterpret_cast<char*> is defined by the ISO standard, the resulting char have unspecified values.
As example, your code:
I have been reading in a lot of places that using reinterpret_cast is bad but they never explain the reason for it. I use it a lot in my code to convert int to char * since I need to send the individual bytes of the int across a network.
<CODE>
char *byteCrcPtr = NULL;
DWORD byteCrc = 0xFF23F2e4;
byteCrcPtr = reinterpret_cast<char *>(&byteCrc);
for(int i=0;i<sizeof(DWORD);i++) {
packet[i]= byteCrcPtr[i];
}
</CODE>
To convert back
<CODE>
DWORD byteCrc = *(reinterpret_cast<DWORD*>(&packet[9]));
</CODE>
Will only work on an homegenous network (i.e. all machines must share the same byte order).
If you want to avoid this problem, you should use htonl and ntohl
Code:
DWORD byteCrc = 0xFF23F2e4; // assume that DWORD is available on all the machines of the network.
DWORD nByteCrc=htonl(byteCrc); // required for using the network (host neutral) byte order.
memcpy(packet, &nByteCrc, sizeof(DWORD)); // a good old memcpy is fine here, std::copy too.
Originally Posted by exterminator
The basic idea is type-safety. reinterpret cast (i will call it RC henceforth) is capable of casting any type to any other type and back successfully.
Not always *successfully*!
reinterpret_cast is more dangerous than that!
The intermediate, mediator, type must have a less strict alignment requirement than the original type.
i.e., on 68000 or PowerPC processors, this code:
Code:
const char* p="hello";
const char* q=reinterpret_cast<const char*>(reinterpret_cast<const int*>(p));
char x=*q; // Undefined behavior : Actually, implementation-specific documentation might give the behavior of this line, but it is not portable!
"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()!
-
July 14th, 2006, 06:24 PM
#6
Re: reinterpret_cast question
reinterpret_cast arbitrary types similar operator [] random access must be careful
Kuphryn
-
July 15th, 2006, 02:19 AM
#7
Re: reinterpret_cast question
Originally Posted by SuperKoko
The intermediate, mediator, type must have a less strict alignment requirement than the original type.
I must agree with this. Here's a good read tht I found : reinterpret_cast
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|