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

dereferencing type-punned pointer will break strict-aliasing rules

I'm developing an application with GCC 3.4 for ARM7.

When doing following:

Expand|Select|Wrap|Line Numbers
  1. T_f32 temp_f32;
  2. T_u32 temp_u32;
  3.  
  4. temp_u32 = *((T_u32 *) &temp_f32);
(where T_u32 is unsinged int, and T_f32 is 32-bit is float)

I'm getting following warning:

dereferencing type-punned pointer will break strict-aliasing rules


It only happens using -O2 optimization (-O0 reports no warning)

What is exactly strict aliasing? So far, I know there's lot of stuff to be taken into account regarding the way ARM aligns pointers. I do the code above to get a temp_u32 containing the four bytes in the float, as they come. Am I going to have trouble?
Jun 20 '08 #1
6 12324
JosAH
11,448 Expert 8TB
Aliasing or anti aliasing is a subject that never made it in any of the C Standards.
The intention was what the compiler could keep track of pointers pointing to
something and that something itself (two aliases). It was shown that a compiler
can't do that and your code is an example thereof.

Unless one of the types has a stricter alignment than the other, you can safely
copy the 32 bits of a float to the 32 bits of your unsigned int. Note that alignment
has nothing to do with aliasing.

Concluding: there's no need to worry; gcc tries to be more law obedient than
the actual law.

kind regards,

Jos
Jun 20 '08 #2
RRick
463 Expert 256MB
Any idea why -O2 optimization would turn on this warning?

My guess is that some code optimization is tiggered by this code.
Jun 20 '08 #3
Wait wait.

I'm not so sure now it's the optimization. The warning appeared when I changed my makefile from debug to release.

The change from -O0 to -O2 is only one of the changes (so, I incorrectly said that it was the only). I'll double check what is the change that actually produces that (on monday, at work), and post it here.
Jun 21 '08 #4
JosAH
11,448 Expert 8TB
Wait wait.

I'm not so sure now it's the optimization. The warning appeared when I changed my makefile from debug to release.

The change from -O0 to -O2 is only one of the changes (so, I incorrectly said that it was the only). I'll double check what is the change that actually produces that (on monday, at work), and post it here.
The DEBUG flag could also have affected the -O flag: suppose the compiler had
figured out that, say, an int* ip was an alias for an int i; in debug mode you
would've been surprised to see variable i being changed if in your opinion just
the value *ip was changed. The compiler could've turned off that strict aliasing
rule. In release mode however, if the above was true, a direct change of variable
i would've been legitimate but your other code (see your OP) breaks that strict
aliasing rule and your compiler warns you for it.

According to the Standard, a compiler is allowed to issue warning diagnostic
messages whenever and whereever it wants so that could explain it all.

kind regards,

Jos
Jun 22 '08 #5
I just add '-fno-strict-aliasing' in Makefile.
Oct 9 '10 #6
Short answer,
struct either{
T_f32 asf32;
T_u32 asu32;
};
struct either e;
e.asu32=temp_u32;
temp_f32=e.asf32;

Or you could use memcpy casting each to char * since
a char* can alias anything.

Long answer, http://dbp-consulting.com/StrictAliasing.pdf, a white paper I wrote because this question comes up over and over. It explains what strict aliasing is and isn't, why developers get confused about it, why it's needed for good optimization, and how to do the sorts of things that people want to do that come afoul of strict-aliasing rules.

Patrick
Dec 30 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

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 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...
0
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption...
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: Tristan | last post by:
Hi all, I'm teaching myself C++ from C++ Primer 4th Ed., but I've not done any programming before and the damn thing has no answers in it! If I pass a reference to cin to a function called...
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
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
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
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
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
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
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...

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.