http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47796
--- Comment #7 from qihua.dai at intel dot com 2011-02-21 13:02:41 UTC ---
Hi,
I used -Wall -O2. But no warning for this situation.
gcc will print strict-aliasing related warning.
struct tmp1_s tmp;
tmp.a = 0xc; // this code line is removed unexpected after -O2 optimization
tmp.d = 0x1; // this code line is removed unexpected after -O2 optimization
data = *((unsigned int *)pTmp);
But, below code can fix above this warning. But this code might be removed by
gcc �CO2 in the situation of this bug.
struct tmp1_s tmp;
unsigned int *pData;
tmp.a = 0xc; // this code line is removed unexpected after -O2 optimization
tmp.d = 0x1; // this code line is removed unexpected after -O2 optimization
pData = (unsigned int *)pTmp;
data = *pData;
In addition, if cannot access bit field structure using an unsigned int, how to
share different bit field structure? For example,
void func(unsigned int data)
data can be bit field structure tmp1_s, or tmp2_s according to bit[31] (0 is
tmp1_s, 1 is tmp2_s)
struct tmp1_s {
unsigned int a : 12;
unsigned int b : 12;
unsigned int c : 7;
unsigned int d : 1;
}
struct tmp2_s {
unsigned int a : 1;
unsigned int b : 12;
unsigned int c : 12;
unsigned int d : 6;
unsigned int e : 1;
}
- Qihua
-----Original Message-----
From: jakub at gcc dot gnu.org [mailto:[email protected]]
Sent: 2011年2月21日 16:29
To: Dai, Qihua
Subject: [Bug c/47796] The code to write to a bit_field data strucuture will be
removed unexpectedly with gcc 4.5.1 -O2 option
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47796
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
CC| |jakub at gcc dot gnu.org
Resolution| |INVALID
--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-02-21
08:29:03 UTC ---
Undefined behavior means anything can happen, so wondering why undefined
behavior doesn't always behave the same doesn't make any sense. And, both gcc
4.4.5 and 4.5.2 do warn about this with -Wstrict-aliasing=1 -O2:
pr47796.c:25: warning: dereferencing type-punned pointer might break
strict-aliasing rules
pr47796.c:31: warning: dereferencing type-punned pointer might break
strict-aliasing rules
Please stop reopening this.
--
Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You reported the bug.