473,289 Members | 2,106 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,289 software developers and data experts.

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 2333
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
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
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
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
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
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
by: ma740988 | last post by:
Consider: # include <iostream> # include <string> # include <map> # include <bitset> # include <vector> using namespace std;
17
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.