473,808 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ and data types

I have a basic question regarding various data types (related to
reinterpret_cas t, 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<uns igned int*> (&i);

//2.
// this will work and uip == 444
uip = reinterpret_cas t<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_cas t 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_cas t do wrt their parameters and what are the rules.
thanks much for any info. have a nice day.

G

Jul 27 '05 #1
2 1624
Gr**********@gm ail.com wrote:
I have a basic question regarding various data types (related to
reinterpret_cas t, 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<uns igned int*> (&i);

//2.
// this will work and uip == 444
uip = reinterpret_cas t<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_cas t 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_cas t 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_cas t, 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_cas t. 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
1942
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, one string represents the data, one string represents the data type. Here's a quick example of what it sort of looks like string data = { "0.0000002342", "12.00234", "42", "5" }; string types = { "EF", "EF", "I", "I" }; //EF is 64 bit float,...
13
3698
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
3194
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 accomplish it. For example: int main void() { while there are more data types { print next data type; }
8
3275
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 convert the vc++ datatypes into c data types or i have to use eqvivalent data types. Can someone give me some idea how to convert or about the equivalent data types? Thanks in Advance,
11
3445
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 difference between data type 'CHAR' and 'TEXT'? When do you use 'VAR' in your datatype word? e.g. VARCHAR ?
7
2863
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 explain me what does the above statement mean? <script runat="server"> Class Clock
18
2761
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 from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and useful tracebacks on the other. I want both! How do you guys do it? What's the pythonic way? Are...
3
3263
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 ). Please suggest me which is the best way of handling the file data. I- Method: ---------------- Store as single line string data's(upto end of file ) in a list and make use of this string list for the entire application.
1
1342
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 unhandled. The data types text and nvarchar are incompatible in the equal to operator." Here is the relevant code. Dim da As New SqlDataAdapter(strSQL, cn) da.SelectCommand.Parameters.AddWithValue("@OrderID", tbOrderID.Text)
0
2964
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 = "select * from " . $table_name . " ORDER BY stateID ASC"; $result = mysql_query($query, $connection) or die("Could not complete database query"); $num = mysql_num_rows($result); if ($num != 0) {
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10628
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...
0
10373
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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
10113
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
9195
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
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
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

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.