473,612 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)==si zeof(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 1907
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)==si zeof(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)==si zeof(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)==si zeof(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)==si zeof(float); does the coincidence
of sizes imply interchangeabil ity?
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(&somet hing[i]);

and

void function(int*);
int something[SIZE];
function(&somet hing[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
3600
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
3044
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 16, B-3010 Leuven, Belgium Bored with EDA the way it is? Check this: http://jandecaluwe.com/Tools/MyHDL/Overview.html
2
1433
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 action on an element in a normal container (normal means everyhing-but-a-pointer), I can do something like this: for_each( coll.begin(), coll.end(), do_something );
2
8893
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 (pseudocode): begin = find(...);
11
1967
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 follows: portb = 0x0f; But when I want to define a register as follows, the code does not
4
2026
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 via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
16
2334
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 cannot be casted to a textbox. The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.
28
2430
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
4383
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 nested for loop recovery of the stored data and properly dereferencing the data. Here is a bit of test code: #include <iostream> #include <string> #include <vector> using namespace std; struct s { string strA;
5
1489
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
8171
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8114
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8615
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8568
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8422
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6081
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4110
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2554
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 we have to send another system
1
1699
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.