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

dereferencing type-punned pointer, redux

We've established that a line of code like:

(void) function( (long *) &(something[i]) );

may (and probably should) generate one of these

warning: dereferencing type-punned pointer will break
strict-aliasing rules

if "something" is of some other type, say "int".

Assume for the sake of argument that elsewhere in the program we have
established that sizeof(int)==sizeof(long), and so we know it is safe
in terms of bytes of storage to keep both int and long in "something".
Then this variant would eliminate the type-punned pointer warning:

int something[SIZE];
int itmp,i;
long ltmp;

/* set i, other code, then */
(void) function( &ltmp); /* expects (long *) argument */
something[i]=(int) ltmp;
I'm guessing that if the temporary variable was not used anywhere else,
then the compiler would usually be smart enough to eliminate it and to
generate code pretty much equivalent to the one that generates the
type punned warning, but it would not generate such a warning because
here the compiler knows what it can get away with to store a long into
the int array.

The question is, is there some other way, within the C language
standard, to inform the compiler which sorts of pointers are equivalent,
so that function() could store directly into something[i] without having
to pass it through a temporary variable?

Regards,

David Mathog
Jul 10 '07 #1
1 1888
David Mathog wrote On 07/10/07 13:29,:
We've established that a line of code like:

(void) function( (long *) &(something[i]) );

may (and probably should) generate one of these

warning: dereferencing type-punned pointer will break
strict-aliasing rules

if "something" is of some other type, say "int".

Assume for the sake of argument that elsewhere in the program we have
established that sizeof(int)==sizeof(long), and so we know it is safe
in terms of bytes of storage to keep both int and long in "something".
On the DeathStation 9000, sizeof(int)==sizeof(long),
but ints are Big-Endian signed magnitude and longs are
Little-Endian ones' complement. (This applies to DS9K
units manufactured on odd-numbered days in months with
"R;" the specifications for other models may differ.)

That is, sizeof(int)==sizeof(long) is a pretty strong
hint that the two are the same "under the hood," but it's
only a hint and not something C guarantees. On many
systems sizeof(int)==sizeof(float); does the coincidence
of sizes imply interchangeability?
Then this variant would eliminate the type-punned pointer warning:

int something[SIZE];
int itmp,i;
long ltmp;

/* set i, other code, then */
(void) function( &ltmp); /* expects (long *) argument */
something[i]=(int) ltmp;
I'm guessing that if the temporary variable was not used anywhere else,
then the compiler would usually be smart enough to eliminate it and to
generate code pretty much equivalent to the one that generates the
type punned warning, but it would not generate such a warning because
here the compiler knows what it can get away with to store a long into
the int array.
Sounds like a remarkably smart compiler. Eliminating
a variable whose address is taken and passed to another
function seems pretty advanced.
The question is, is there some other way, within the C language
standard, to inform the compiler which sorts of pointers are equivalent,
so that function() could store directly into something[i] without having
to pass it through a temporary variable?
Yes! There is a perfectly safe, Standard-conforming,
100% guaranteed, pure-as-the-driven-snow way to tell the
compiler that it's all right to use a long*, or all right
to store into an int. In fact, there are two ways:

void function(long*);
long something[SIZE];
function(&something[i]);

and

void function(int*);
int something[SIZE];
function(&something[i]);

In other words: Rather than searching for a foolproof
way to lie to the compiler and never get caught (are there
any politicians in your family tree?), it is better to
make a clean breast and not lie at all. Or in still other
other words: If you are so all-fired certain that int and
long are equivalent, why insist on using both spellings
instead of settling on one or the other?

--
Er*********@sun.com

Jul 10 '07 #2

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

Similar topics

8
by: Owen Funkhouser | last post by:
Maybe someone can help me out here: $name1 = "Owen"; $name2 = "Funkhouser"; for ($i = 1; $i < 3; $i++ ) { echo "<!-- name$i = $name$i -->\n"; } I'd like to see:
8
by: Jan Decaluwe | last post by:
Is there a way to dereference a cell object (that is, get the object that it references to) in Python? Regards, Jan -- Jan Decaluwe - Resources bvba - http://jandecaluwe.com Losbergenlaan...
2
by: Matthias | last post by:
Hi, again, I have a problem using containers of pointers (I'm near to the point to drop them alltogether and work on usual containers instead...). The problem is, if I want to perform some...
2
by: Matthias Kaeppler | last post by:
Hello, I was wondering, does dereferencing past-the-end iterators yield undefined behavior? Especially, is the result of calling an STL algorithm on an empty range undefined? For example...
11
by: jlara | last post by:
Working on an embedded controller, I generally find it convenient to define the following de-referenced pointer: #define portb (*(unsigned char *)(0x03u)) This enable me to write to a port as...
4
by: Max Sandman | last post by:
Say I've got a control c = textBox1 and a string that is a property of c, say string s = "Text". Is there a way in C# to use c and s to get the contents of textbox1.Text? sandman *** Sent...
16
by: Michael Maes | last post by:
Hi, How would I handle Dereferencing in vb.Net 2003. Something like: Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox) This sadly won't work because a type of string...
28
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
4
by: Caudata | last post by:
I am by no means an experienced c++ programmer, but I am trying to use a vector of vectors because it is convenient to store some strings while parsing a text file. I am having trouble with the...
5
by: sebastian | last post by:
I have a dereferencing template that I believe to be implemented correctly and complete, but I would like to have it checked for correctness to be sure. is this the right forum for such a thing?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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
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,...

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.