473,506 Members | 17,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unusual casting

I'm looking for the name of the following casting style in order to do
some reading around on it and why it is sometimes used.

unsigned long long ull = 0;
void * ptr = 0;

ull = *(unsigned long long*)&ptr;

As opposed to the more usual casting:

ull = (unsigned long long)ptr;

I think it is just to silence compiler warnings, but if anyone has links
with further information, that would be great.

William
Nov 23 '06 #1
7 2637
William S Fulton wrote:
I'm looking for the name of the following casting style in order to do
some reading around on it and why it is sometimes used.

unsigned long long ull = 0;
void * ptr = 0;

ull = *(unsigned long long*)&ptr;

As opposed to the more usual casting:

ull = (unsigned long long)ptr;

I think it is just to silence compiler warnings, but if anyone has links
with further information, that would be great.
If you're dealing with code which is meant to be portable, simply don't
use the first form. The behaviour is undefined in standard C, and even
if unsigned long long is of the same size as void * and has no padding
bits, it will not do what's expected in some compilers (for example,
GCC).

Also, for the second form, using intptr_t or uintptr_t would be a
better idea if it's available. And when it's not available, unsigned
long long probably won't work either (but unsigned long might, if
you're dealing with C90).

Nov 23 '06 #2
William S Fulton <ws*@fultondesigns.co.ukwrites:
I'm looking for the name of the following casting style in order to do
some reading around on it and why it is sometimes used.

unsigned long long ull = 0;
void * ptr = 0;

ull = *(unsigned long long*)&ptr;

As opposed to the more usual casting:

ull = (unsigned long long)ptr;

I think it is just to silence compiler warnings, but if anyone has links
with further information, that would be great.
Are you sure that's the actual code? It's treating an object of type
void* as if it were an object of type unsigned long long. There's no
reason to assume that they're going to be the same size.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '06 #3
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>William S Fulton <ws*@fultondesigns.co.ukwrites:
> unsigned long long ull = 0;
void * ptr = 0;
ull = *(unsigned long long*)&ptr;
As opposed to the more usual casting:
ull = (unsigned long long)ptr;
>Are you sure that's the actual code? It's treating an object of type
void* as if it were an object of type unsigned long long. There's no
reason to assume that they're going to be the same size.
Looks like a conflation of type-punning and the fact that pointers
can be converted to void* and back to the same type without any harm.

But the special properties of void* do not apply to pointers to
void* (i.e., void**), so converting that to unsigned long long*
is not certain to produce any kind of meaningful result -- and
the leading * dereferences the result, so it's trying to
get at an unsigned long long value in the space that is there for
a void* (a pointer), which is also pretty dubious. And it's doing
this for a NULL pointer, which isn't certain to have an all-zeros
representation. Perhaps someone's trying to test whether
the void* NULL pointer is in fact all zeros?? Not a particularily
useful test, since other NULL pointers might have different
internal values (as long as they -compare- properly.)

So I agree, the code is broken or else it doesn't actually look
exactly like that.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 23 '06 #4
Keith Thompson wrote:
William S Fulton <ws*@fultondesigns.co.ukwrites:
>I'm looking for the name of the following casting style in order to do
some reading around on it and why it is sometimes used.

unsigned long long ull = 0;
void * ptr = 0;

ull = *(unsigned long long*)&ptr;

As opposed to the more usual casting:

ull = (unsigned long long)ptr;

I think it is just to silence compiler warnings, but if anyone has links
with further information, that would be great.

Are you sure that's the actual code? It's treating an object of type
void* as if it were an object of type unsigned long long. There's no
reason to assume that they're going to be the same size.
The approach is used where the unsigned long long is a JNI jlong. So it
is a way of storing a C pointer in a Java long (an unsigned 64 bit
number on all platforms). Even if the void * is 32 bits, it still works,
as it gets cast back into a void * when passed from the Java layer to C
layer. Perl does the same thing converting between pointers and
integers. None of this is 100% portable in theory, but is in practice on
all the systems that support Perl and Java. That is if you aren't using
the latest gcc with optimisation turned on as the cast breaks aliasing
rules. The main problem is that C does not provide a 100% portable way
of casting a pointer into an integer and back again.

Still, that is a bit of digression and I am really looking to find out
if this type of casting has a name. I think I read a long time ago, it
was some sort of universal type of cast that would never fail to compile
on all systems, or something like.

William
Nov 23 '06 #5
William S Fulton <ws*@fultondesigns.co.ukwrites:
Keith Thompson wrote:
>William S Fulton <ws*@fultondesigns.co.ukwrites:
>>I'm looking for the name of the following casting style in order to do
some reading around on it and why it is sometimes used.

unsigned long long ull = 0;
void * ptr = 0;

ull = *(unsigned long long*)&ptr;

As opposed to the more usual casting:

ull = (unsigned long long)ptr;

I think it is just to silence compiler warnings, but if anyone has links
with further information, that would be great.

Are you sure that's the actual code? It's treating an object of type
void* as if it were an object of type unsigned long long. There's no
reason to assume that they're going to be the same size.
Please don't send me e-mail copies of Usenet followups.

[...]
Still, that is a bit of digression and I am really looking to find out
if this type of casting has a name. I think I read a long time ago, it
was some sort of universal type of cast that would never fail to compile
on all systems, or something like.
It's a form of type punning, i.e., pretending that an object (memory
region) holding something of one type holds a value of a different
type.

The difference between
ull = *(unsigned long long*)&ptr;
and
ull = (unsigned long long)ptr;
is that the first pretends that the object ptr holds a value of type
unsigned long long, whereas the second *converts* the value of ptr
(which is of type void*) to unsigned long long. A conversion from a
pointer type to an integer type is implementation-defined; it may just
reinterpret the same bit pattern, or it may perform some non-trivial
conversion (as conversions between integers and floats do). On *most*
systems, conversion between a pointer and an integer *of the same
size* just copies the bits.

Note that if, for example, void* is 32 bits and unsigned long long is
64 bits, the statement
ull = *(unsigned long long*)&ptr;
attempts to read 64 bits from a 32-bit object. This invokes undefined
behavior; most likely, ull will contain 32 bits from the pointer
object and another 32 bits of whatever garbage happened to be stored
next to it. And these could be in either order. On the other hand,
converting from void* to unsigned long long, though it's
implementation-defined doesn't attempt to read garbage. Most likely
(if sizeof(unsigned long long) sizeof(void*)) it will just copy the
bits of the pointer and zero-extend the result.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '06 #6
William S Fulton schrieb:
Keith Thompson wrote:
>>William S Fulton <ws*@fultondesigns.co.ukwrites:
<snip>
The approach is used where the unsigned long long is a JNI jlong. So it
is a way of storing a C pointer in a Java long (an unsigned 64 bit
number on all platforms).
<snip>
William
pedantic:
the only unsigned Java type is char (which is not a C char).

Wolfgang
Nov 24 '06 #7
riedel wrote:
William S Fulton schrieb:
>Keith Thompson wrote:
>>William S Fulton <ws*@fultondesigns.co.ukwrites:
<snip>
>The approach is used where the unsigned long long is a JNI jlong. So it
is a way of storing a C pointer in a Java long (an unsigned 64 bit
number on all platforms).
<snip>
>William
pedantic:
the only unsigned Java type is char (which is not a C char).
Yeah, sorry, that was a typo and should read 'signed 64 bit number'.

William
Nov 24 '06 #8

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

Similar topics

4
3442
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
4
2261
by: Larry R Harrison Jr | last post by:
I have an Access 2000/XP with an unusual autonumber scheme; I can't figure out how it generates its very unique value. I have the database temporarily located at this website: ...
231
22922
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
3640
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
30750
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
3
1681
by: Farooq Khan | last post by:
why does Response.Write in a method of code-beind class when called from inpage code (i.e in <%---%>), after creating object of that class, fails when called while it works perfectly ok while...
7
13628
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
2200
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
10
1403
by: sheldonlg | last post by:
I got an unusual request. One customer wants a password/access made available to a user that is valid for only, say, ten minutes. I know that I can enforce this by having a revalidation of the...
0
7105
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
7308
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,...
1
7023
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...
0
7479
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...
0
5617
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,...
0
4702
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
3188
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
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
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.