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

pointer type casting


Can anyone explain the following codes?
especially the cast of pointer type.
What does it really mean. Thanks.

float f1;
unsigned long l1;

f1=4.5;

l1=*(unsigned long*)&f1;

Mar 25 '07 #1
9 6373
ch*******@gmail.com wrote:
Can anyone explain the following codes?
especially the cast of pointer type.
What does it really mean. Thanks.

float f1;
unsigned long l1;

f1=4.5;

l1=*(unsigned long*)&f1;
It means, among other things, that the coder is trying to make his
program commit suicide.

On the surface, he is taking the address of the float f1,
using it as a pointer to an unsigned long,
and storing what ever is pointed at into an unsigned long.

Here is why this playing fast-and-loose with these types is a bad thing:
1) There is no requirement for floats to be stored in any particular
way. The same bit pattern could mean different values for f1 on
different platforms or even the same platform and a different compiler.
2) There is no requirement for unsigned longs to be stored in any
particular way. The same bit pattern could mean different values for l1
on different platforms or even the same platform and a different compiler.

1 & 2) together mean that you don't have a clue what it means to treat a
sequence of bits stored in a float as an unsigned long.

3) [and this is the killer] There is no requirement about the relative
amounts of space that a float and an unsigned long consume. In
particular, it would not be surprising for floats to be shorter than
unsigned longs. That means that using the address of a float as the
address of an unsigned long involves using memory which is not part of
the float object. This is a very bad thing.

This kind of code depends on a particular representation of floats, a
particular representation of unsigned longs, and on the assumption that
sizeof(float) >= sizeof(unsigned long) and probably that sizeof(float)
== sizeof(unsigned long).

This is the kind of code that should get a programmer fired in many
cases. Even if the specification of a program is, at the moment, for a
particular piece of hardware with a particular software configuration,
writing code like this depends on there never being a hardware or
software modification. This is a reasonable bet for embedded software
at best.

Mar 25 '07 #2
ch*******@gmail.com wrote:
>
Can anyone explain the following codes?
especially the cast of pointer type.
What does it really mean. Thanks.

float f1;
unsigned long l1;

f1=4.5;

l1=*(unsigned long*)&f1;
Most likely, it means that the programmer didn't know that
ll = fl;
is the correct way to assign a value from
that float object to that unsigned long object.

One of the possible problems with your code example
is that &f might not be aligned for a long type
which means that ((unsigned long*)&f1) might be undefined.

--
pete
Mar 25 '07 #3
"ch*******@gmail.com" wrote:
>
Can anyone explain the following codes? especially the cast of
pointer type. What does it really mean. Thanks.

float f1;
unsigned long l1;

f1=4.5;

l1=*(unsigned long*)&f1;
It means the programmer is incompetent, and possibly also
incontinent. The last statement is meaningless, and invokes
undefined behaviour.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 25 '07 #4
pete <pf*****@mindspring.comwrites:
ch*******@gmail.com wrote:
>Can anyone explain the following codes?
especially the cast of pointer type.
What does it really mean. Thanks.

float f1;
unsigned long l1;

f1=4.5;

l1=*(unsigned long*)&f1;

Most likely, it means that the programmer didn't know that
ll = fl;
is the correct way to assign a value from
that float object to that unsigned long object.
I don't think that's the most likely explanation. I can easily
imagine a programmer not knowing whether a cast is necessary when
assigning a float value to an unsigned long object, but the use of
pointer casts tells me that the programmer was deliberately trying to
examine the representation of f1 as if it were of type unsigned long.

In some contexts, that might even be a reasonable thing to do, though
it's not necessarily the best way to do it, because ...
One of the possible problems with your code example
is that &f might not be aligned for a long type
which means that ((unsigned long*)&f1) might be undefined.
Here's a mostly reasonable program that incorporates the above code:

#include <stdio.h>
int main(void)
{
if (sizeof (float) == sizeof (unsigned long)) {
float f1 = 4.5;
unsigned long l1;

l1 = *(unsigned long*)&f1;
printf("f1 = %g, l1 = %lx\n", f1, l1);
if (l1 == 0x40900000) {
puts("Looks like 32-bit IEEE floating-point");
}
else {
puts("Unknown format");
}
}
else {
puts("Size mismatch");
}
return 0;
}

As you point out, alignment is a potential problem, so I'd probably
use memcpy() rather than pointer tricks. I'd probably also check more
than one value, and try harder to find a matching integer type if
unsigned long doesn't match -- but maybe I only care about some
limited set of implementations.

In fact, I have a program I use sometimes that does something similar
to the above, but I look at the floating-point object as an array of
unsigned char, whose values I compare to known values for several
floating-point values. It recognizes 32-bit, 64-bit, and 96-bit IEEE
(distinguishing between big-endian and little-endian), as well as
SPARC/HPPA and SGI 128-bit floating-point formats, plus 64-bit and
128-bit Cray floating-point. (Since I don't do exhaustive testing, a
false positive is always a remote possibility.)

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 25 '07 #5
CBFalconer <cb********@yahoo.comwrites:
"ch*******@gmail.com" wrote:
>>
Can anyone explain the following codes? especially the cast of
pointer type. What does it really mean. Thanks.

float f1;
unsigned long l1;

f1=4.5;

l1=*(unsigned long*)&f1;

It means the programmer is incompetent, and possibly also
incontinent. The last statement is meaningless, and invokes
undefined behaviour.
It certainly invokes undefined behavior, but it's not meaningless.
Its meaning is to examine the value of f1 as if it were of type
unsigned long rather than float. There are numerous ways it can go
wrong, but it *can* do just what the author probably intended it to
do. (Which is, in a very real sense, worse than being completely
meaningless or always causing a trap.)

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 25 '07 #6
On Mar 25, 2:28 pm, Keith Thompson <k...@mib.orgwrote:
pete <pfil...@mindspring.comwrites:
cheesi...@gmail.com wrote:
Can anyone explain the following codes?
especially the cast of pointer type.
What does it really mean. Thanks.
float f1;
unsigned long l1;
f1=4.5;
l1=*(unsigned long*)&f1;
Most likely, it means that the programmer didn't know that
ll = fl;
is the correct way to assign a value from
that float object to that unsigned long object.

I don't think that's the most likely explanation. I can easily
imagine a programmer not knowing whether a cast is necessary when
assigning a float value to an unsigned long object, but the use of
pointer casts tells me that the programmer was deliberately trying to
examine the representation of f1 as if it were of type unsigned long.

In some contexts, that might even be a reasonable thing to do, though
it's not necessarily the best way to do it, because ...
what is the alternative?
>From the code, how can we tell what is the l1? Does l1 store the
address of f1?
>
One of the possible problems with your code example
is that &f might not be aligned for a long type
which means that ((unsigned long*)&f1) might be undefined.
Could you tell me more about the undefined behavior?

thanks.
>
Here's a mostly reasonable program that incorporates the above code:

#include <stdio.h>
int main(void)
{
if (sizeof (float) == sizeof (unsigned long)) {
float f1 = 4.5;
unsigned long l1;

l1 = *(unsigned long*)&f1;
printf("f1 = %g, l1 = %lx\n", f1, l1);
if (l1 == 0x40900000) {
puts("Looks like 32-bit IEEE floating-point");
}
else {
puts("Unknown format");
}
}
else {
puts("Size mismatch");
}
return 0;

}

As you point out, alignment is a potential problem, so I'd probably
use memcpy() rather than pointer tricks. I'd probably also check more
than one value, and try harder to find a matching integer type if
unsigned long doesn't match -- but maybe I only care about some
limited set of implementations.

In fact, I have a program I use sometimes that does something similar
to the above, but I look at the floating-point object as an array of
unsigned char, whose values I compare to known values for several
floating-point values. It recognizes 32-bit, 64-bit, and 96-bit IEEE
(distinguishing between big-endian and little-endian), as well as
SPARC/HPPA and SGI 128-bit floating-point formats, plus 64-bit and
128-bit Cray floating-point. (Since I don't do exhaustive testing, a
false positive is always a remote possibility.)

--
Keith Thompson (The_Other_Keith) k...@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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Mar 26 '07 #7
ch*******@gmail.com wrote:
On Mar 25, 2:28 pm, Keith Thompson <k...@mib.orgwrote:
>>pete <pfil...@mindspring.comwrites:
>>>cheesi...@gmail.com wrote:

Can anyone explain the following codes?
especially the cast of pointer type.
What does it really mean. Thanks.
>>>>float f1;
unsigned long l1;
>>>>f1=4.5;
>>>>l1=*(unsigned long*)&f1;
>>>Most likely, it means that the programmer didn't know that
ll = fl;
is the correct way to assign a value from
that float object to that unsigned long object.

I don't think that's the most likely explanation. I can easily
imagine a programmer not knowing whether a cast is necessary when
assigning a float value to an unsigned long object, but the use of
pointer casts tells me that the programmer was deliberately trying to
examine the representation of f1 as if it were of type unsigned long.

In some contexts, that might even be a reasonable thing to do, though
it's not necessarily the best way to do it, because ...


what is the alternative?
That depends what the programmer intended.
>>From the code, how can we tell what is the l1? Does l1 store the
address of f1?
No, it stores the bit pattern stored at the address of f1.
>
>>>One of the possible problems with your code example
is that &f might not be aligned for a long type
which means that ((unsigned long*)&f1) might be undefined.

Could you tell me more about the undefined behavior?
Consider the case where float and unsigned long are different sizes, or
have differing alignment rules.

Please trim stuff you aren't responding to.

--
Ian Collins.
Mar 26 '07 #8
"ch*******@gmail.com" wrote:
>
.... snip ...
>
Could you tell me more about the undefined behavior?
Yes. It is undefined. Any behaviour is allowed. This includes
insulting Mohammed, starting WWIII, global warming, species
extinction, nasal expectoration of demons, etc. Other restrictions
may apply, but those are beyond the scope of the C language.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 26 '07 #9

<ch*******@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...
Could you tell me more about the undefined behavior?

thanks.
When the Standard says a behaviour is undefined, the compiled program is
allowed to do *anything*, and *whatever* it does (even if it were formatting
your hard disk) will not make the compiler non-conformant.
Usually it won't format your hard disk, and it will either report an error
or do something reasonable (maybe even including "exactly what you meant to
do"). I find hard to imagine i = ++i; doing something else than increasing i
or complaining (whereas i = i++; is more ambiguous: increase i by one and
restore it to its old value, or assign it to itself and then increase it by
one?), but you should NOT rely on this if you want your program to be
portable.
Mar 26 '07 #10

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

Similar topics

16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
10
by: lovecreatesbeauty | last post by:
Why (type*)pointer isn't equal to *(type**)pointer, In the code snippet, it shows that: (int *) == (int **) , (int *) != (*(int **)) . Does type-casting change the address? or...
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
5
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting...
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
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...

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.