473,388 Members | 1,335 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,388 software developers and data experts.

c++ and data types

I have a basic question regarding various data types (related to
reinterpret_cast, static_cast).
Lets say I have some declarations thus;

int i = 444;
unsigned int* uip;

I then do something like;
// 1.
// compiler error.... can't cast from int* to unsigned int*
//uip = static_cast<unsigned int*> (&i);

//2.
// this will work and uip == 444
uip = reinterpret_cast<unsigned int*> (&i);
//3.
// this will give me i3 == 666 (I know that if i2 <0 i3 will be
// rubbish)

int i2=666;
unsigned int i3;
i3=static_cast<int> (i2);
Could anybody tell me whats going on under the covers such that 1 fails
and 2 succeeds. I guess the question is what do the templates
static_cast and reinterpret_cast do at a low level.
Why does the static_cast in 1. fail when using pointers but succeed in
3. when I'm not dealing with pointers.

I reckon my question is basically, what sort of checking do static_cast
and reinterpret_cast do wrt their parameters and what are the rules.
thanks much for any info. have a nice day.

G

Jul 27 '05 #1
2 1608
Gr**********@gmail.com wrote:
I have a basic question regarding various data types (related to
reinterpret_cast, static_cast).
Lets say I have some declarations thus;

int i = 444;
unsigned int* uip;

I then do something like;
// 1.
// compiler error.... can't cast from int* to unsigned int*
//uip = static_cast<unsigned int*> (&i);

//2.
// this will work and uip == 444
uip = reinterpret_cast<unsigned int*> (&i);
//3.
// this will give me i3 == 666 (I know that if i2 <0 i3 will be
// rubbish)

int i2=666;
unsigned int i3;
i3=static_cast<int> (i2);
Could anybody tell me whats going on under the covers such that 1 fails
and 2 succeeds. I guess the question is what do the templates
static_cast and reinterpret_cast do at a low level.
Why does the static_cast in 1. fail when using pointers but succeed in
3. when I'm not dealing with pointers.

I reckon my question is basically, what sort of checking do static_cast
and reinterpret_cast do wrt their parameters and what are the rules.
thanks much for any info. have a nice day.

G


static_cast asks the compiler to convert a value of one type to a value
of another type according to well defined rules. Rules are defined in
the standard for how to convert each built in type to another built in
type. You can extend the set of rules to user defined types in a few
different ways. You can define a constructor (without the explicit
keyword) that takes a type, example:

class A {
public:
A(int x) {}
} ;
A a = static_cast<A>(3) ;
You also define operators, example:
class A
{
public :
operator int()
{ return 42 ; }
} ;

A a ;
int x = static_cast<int>(a) ;
Of course, for the above examples, casting isn't actually needed at all,
but the goal here is to try to explain what static_cast is doing. It is
looking for some rule to convert the type you give it to the type you
say it should be. If there isn't some well defined rule for doing so,
the compiler considers it an error.

Now, as far as the compiler is concerned, "int" and "unsigned int" are
two different types. While the conversion between the two is well
defined, a _pointer_ to one type has no business pointing to an object
of the other type. In fact, about the only time the compiler can safely
allow a pointer to one type to point to an object of a different type is
when a base class pointer is pointing to an object of a derived class.
So, since the compiler doesn't have any rules for converting an "int *"
to an "unsigned int *", it decides that the cast must be an error.

reinterpret_cast, on the other hand, is your mechanism for telling the
compiler that you really do know what you are doing and you really do
want to treat an object of one type as an object of another. The
standard doesn't make many guarantees about what actually happens to a
value when you do a reinterpret_cast. It might change, or it might not.
The only real guarantees you have are that if you cast a pointer to
an integer type, and then cast the integer back to a pointer of the same
type, then the pointer value is unchanged, and that the result of
casting a null pointer is a null pointer of the target type.

-Alan
Jul 27 '05 #2
purrfect! thanks so much for that response alan. Cleared it up for me
nicely. have a nice day

GrahamO

Jul 27 '05 #3

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

Similar topics

3
by: GGG | last post by:
I have a situation where at tool is passing me a large array of strings that I need to process in a particular type of data. Each item in the array gets to me as a pair of std::strings, basically,...
13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
8
by: ramu | last post by:
Hi, I want to call a vc++ function from a c program on linux. So when I do this dosen't the VC++ datatypes differ with c datatypes. Because we don't have some vc++ data types in c. I have to...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
7
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
3
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). ...
1
by: Alex | last post by:
Hi, I have three different data types coming out of my SQL tables that I need to align with my VB code and the data set properties. I'm getting an error message that reads "SQL exception...
0
by: Hags007 | last post by:
I have a XML file I am working with. This file has been created by hand and I now need to develop a PHP script that will create it in the same format. Here is what I have thus far: $query =...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.