473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why is there a need of reinterpret_cast ?

can someone give an example where static_cast fails during compilation and one would be forced to use reinterpret_cast.
Nov 27 '09 #1
3 3326
Banfa
9,065 Expert Mod 8TB
Any time you need to take an integer type and interpret it as a pointer. This does not happen much on PC platforms (Linux, Windows Mac) because you rarely know the actual address of any bits of hardware.

However on embedded platforms it is something you do a fair bit because the various bits of hardware are often mapped into memory at a fixed address, you know the actual value of that address but to access the register at that address you often need to cast that number to a pointer.

reinterpret_cast is one of those facilities of C++ that you should use sparingly and only when you really know it is the right thing to do.
Nov 27 '09 #2
Looking for some simple example which I can run and see on linux machine, where static cast will fail and reinterpret case will work fine.
Nov 27 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
static_cast will only work when you cast between related types. That is, casting a derived class pointer to a base class pointer.

reinterpret_cast will permit a cast between any two pointers.

Therefore, if your static_cast is between unrelated types then it will fail at compile type but the same cast will succeed using a reinterpret_cast.

Please note: ALL of this happens at compile time. There are no run-time checks.

It is dynamic_cast that is a static_cast with a run_time test.

You don't mention dynamic_cast so I am at a loss as to how the run-time applies to static_cast and reinterpret_cast.
Nov 27 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
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. ...
5
by: Christopher Benson-Manica | last post by:
enum foo {a,b,c,d}; int main() { unsigned int bar=reinterpret_cast< unsigned int >( a ); return bar; } g++ rejects the code based on the reinterpret_cast, so I presume it is not legal. ...
4
by: Alex Vinokur | last post by:
Should 'delete below (after casting to void*)' work fine? ------------------------------------------ char* p1 = new (nothrow) char; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1);...
8
by: asdf | last post by:
for (int k=static_cast<int>(box_.min_z()); k<=static_cast<int>(box_.max_z()); k++) warning: converting to `int' from `double' thanks.
7
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....
27
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...
3
by: J.M. | last post by:
I have data in a double array of length 2N, which actually represents complex numbers with real and imaginary parts interlaced. In other words, elements in this array with even indices represents...
2
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
by: Unpopular | last post by:
void directory::modification()//??????????? { clrscr(); cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @ @@@@@@ "; cout<< "\n\t=====@ @ @ @ @ @ @@...
15
by: saurabh | last post by:
I am trying to understand reinterpret_cast,here is what i tried, #include<iostream> #include<cstdio> using namespace std; int main() { int x=1; int* y=&x;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.