473,509 Members | 11,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More of a raw kind of cast


Before I begin, here's a list of assumptions for this particular example:

(1) unsigned int has no padding bits, and therefore no invalid bit-
patterns or trap representations.
(2) All types have the same alignment requirements.
(3) sizeof(double) >= sizeof(unsigned)

=====================================

We can cast from double to unsigned int as follows:

int main()
{
double d = 672.23;

unsigned i = d;
}
However, if we want a raw kind of cast, i.e. simply access the double's
data in memory as if it were an unsigned int, then we can write:

int main()
{
double d = 672.23;

unsigned i = *reinterpret_cast<unsigned*>( &d );
}

However, my question is this:

Do we have to bother with pointers as in the previous snippet, or can
we use references?

int main()
{
double d = 672.23;

unsigned i = reinterpret_cast<unsigned&>( d );
}

--

Frederick Gotham
Jun 29 '06 #1
5 2344
On Thu, 29 Jun 2006 19:15:52 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote in comp.lang.c++:

Before I begin, here's a list of assumptions for this particular example:

(1) unsigned int has no padding bits, and therefore no invalid bit-
patterns or trap representations.
(2) All types have the same alignment requirements.
(3) sizeof(double) >= sizeof(unsigned)

=====================================

We can cast from double to unsigned int as follows:
No, you can't. A cast is ONLY performed by a cast operator, either
the original C cast, or one of the new cast operators defined in C++.
int main()
{
double d = 672.23;

unsigned i = d;
}
The code above performs a conversion, specifically an automatic
conversion on assignment. There is no cast involved. A cast is an
EXPLICIT conversion, defined as such by both C and C++. Some
conversions occur automatically in expressions, others require a cast
operator.

We CAN cast from double to unsigned like this:

int main()
{
double d = 672.23;
unsigned i = (double)d; // other C++ casts could be used
}

This shows that you can use a cast operator to specifically perform a
conversion that would occur automatically in the expression anyway.
However, if we want a raw kind of cast, i.e. simply access the double's
data in memory as if it were an unsigned int, then we can write:
This is not a cast either. What you are doing is trying to access the
raw underlying bits of an object, with an lvalue of a different type.
int main()
{
double d = 672.23;

unsigned i = *reinterpret_cast<unsigned*>( &d );
}

However, my question is this:

Do we have to bother with pointers as in the previous snippet, or can
we use references?
First, the answer is no.

Second, you are not actually having to "bother with pointers", you are
bothering with addresses, which is the name of the type of value held
by a pointer.

The unary '&' operator generates the address of its operand. The cast
takes that address rvalue, which has the type "address of double", and
convert it to an rvalue which has the type "address of unsigned int".
Most likely, there is no change in the bitwise representation of the
address, merely in the number of bits read from memory during the
lvalue to rvalue conversion and how they are interpreted.

No actual pointer exists or is created.
int main()
{
double d = 672.23;

unsigned i = reinterpret_cast<unsigned&>( d );
}


This is trying to cast a double to a reference to int, which quite
impossible.

I don't know what you think the overhead of the pointer cast is. You
are only changing how the address is used to access memory, not
actually create a pointer.

Consider this variation on your second and third examples:

int main()
{
double d1 = 672.23, d2 = 666.89;
unsigned i1 = d1;
unsigned i2 = *reinterpret_cast<unsigned*>(&d2);
}

Do you think the compiler will generate more machine instructions for
the second than it does for the first?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 29 '06 #2
Jack Klein posted:

What you are doing is trying to access the raw underlying bits of an
object, with an lvalue of a different type.

Indeed.

My question is very simple. If we have an object like as follows:

double d;

Then are the following two expressions equivalent?

(1) *reinterpret_cast<unsigned*>( &d )

(2) reinterpret_cast<unsigned&>( d )

--

Frederick Gotham
Jun 29 '06 #3
Frederick Gotham wrote:
...
My question is very simple. If we have an object like as follows:

double d;

Then are the following two expressions equivalent?

(1) *reinterpret_cast<unsigned*>( &d )

(2) reinterpret_cast<unsigned&>( d )
...


Yes, they are. The equivalence of the two is explicitly stated in 5.2.10/10.

I don't know why Jack is saying that the second one is an attempt to
cast a 'double' value to reference type. When an lvalue is used as an
argument for an 'reinterpret_cast' to reference type, the argument is
interpreted as a "reference" value (as a 'double&' in your example, not
as a 'double' rvalue), which is then converted to the target reference type.

--
Best regards,
Andrey Tarasevich
Jun 29 '06 #4
Jack Klein wrote:
On Thu, 29 Jun 2006 19:15:52 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote in comp.lang.c++:
int main()
{
double d = 672.23;

unsigned i = d;
}


The code above performs a conversion, specifically an automatic
conversion on assignment.


Surely you mean on initialization?

Luke

Jun 30 '06 #5
Luke Meyers posted:
Jack Klein wrote:
On Thu, 29 Jun 2006 19:15:52 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote in comp.lang.c++:
> int main()
> {
> double d = 672.23;
>
> unsigned i = d;
> }


The code above performs a conversion, specifically an automatic
conversion on assignment.


Surely you mean on initialization?

Indeed I do.

However we're dealing with intrinsic POD's, so there's no difference.

Notheless, yes, I should have said "initialisation".
--

Frederick Gotham
Jun 30 '06 #6

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

Similar topics

303
17421
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
24
3047
by: Kari Laitinen | last post by:
I have written a computer programming book that uses C++. The book is for beginners in the field of computer programming, but it might give useful ideas also for more experienced programmers....
2
2873
by: STB | last post by:
ciao a tutti... ci risono... devo migliorare delle performance di accesso ad una tabella... la tabella non ha indice primario, ne altri indici... sulla tabella ci accedo con select di questo...
3
2290
by: Lionel B | last post by:
In an overloaded output operator for a class I want to detect ostream manipulators such as flush, endl, ends, etc. I am stymied by how to do this; here's a minimal program indicating my problem: ...
0
3573
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
7
9133
by: ma740988 | last post by:
Consider: # include <iostream> # include <string> # include <map> # include <bitset> # include <vector> using namespace std;
17
2390
by: Chen Shusheng | last post by:
Hi all, In fact, I want to let my memory run out. And see what will happen. My system is windowsXp. Memory is 256M.I think my cdes will apply more memory than I have. Codes are below: ...
13
2830
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
1
2222
by: Aleck | last post by:
Hie. I have a trigger that monitors changes to my table fields but I get an error saying subquery returned more than one value.Below is the code for my trigger, hope you will figure out whats...
0
7137
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
7416
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...
0
7506
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...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
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...
0
3218
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...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
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 ...
1
779
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.