hi,

let's look at code below
typedef union
{
unsigned int n32bits;
struct
{
char p1 : 1;
char p2 : 1;
char p3 : 1;
unsigned int p4 : 29;
}s;
}u;

u u1;
u1.s.p1 = 0;
u1.s.p2 = 1;
u1.s.p3 = 1;
u1.s.p4 = 1;

what value should have u1.n32bits ??
u1.n32bits =
gcc 0x0000000E // ok,
vc 0x00000106 // error, p4 is inplaced!!

Does someone know what is going on here?

--
Marcin Gabryszewski
G DATA Software
www.gdata.pl

address:<FirstName><dot><Surname><at><gdata><dot><pl>

Re: bug in cl compiler ?? by Ulrich

Ulrich
Fri Oct 27 03:13:53 CDT 2006

Marcin Gabryszewski wrote:
> typedef union
> {
> unsigned int n32bits;
> struct
> {
> char p1 : 1;
> char p2 : 1;
> char p3 : 1;
> unsigned int p4 : 29;
> }s;
> }u;
>
> u u1;
> u1.s.p1 = 0;
> u1.s.p2 = 1;
> u1.s.p3 = 1;
> u1.s.p4 = 1;
>
> what value should have u1.n32bits ??

1. You are not allowed to write to one field of a union and read from the
other, according to the C standard. It can be made to work with most
compilers though if some care is taken.
2. Bitfields are only packed up to the size of their base type. Therefore,
you had better use the same for all if you want them packed together.
3. Assuming that 'unsigned int' is 32 bit is just stupid as there is no
guarantee for that. Use an explicitly sized integer like uint32_t (C99) or
UINT32 (win32).
4. The order in those bitfields is even dependant on the endianess!
5. assert(sizeof u == sizeof u.n32bits);
6. [C++ only] No need to create an anonymous union and then use typedef to
assign a name to it.

Uli


Re: bug in cl compiler ?? by Marcin

Marcin
Fri Oct 27 03:36:10 CDT 2006

Ulrich Eckhardt napisaÅ?(a):
> Marcin Gabryszewski wrote:
>> typedef union
>> {
>> unsigned int n32bits;
>> struct
>> {
>> char p1 : 1;
>> char p2 : 1;
>> char p3 : 1;
>> unsigned int p4 : 29;
>> }s;
>> }u;
>>
>> u u1;
>> u1.s.p1 = 0;
>> u1.s.p2 = 1;
>> u1.s.p3 = 1;
>> u1.s.p4 = 1;
>>
>> what value should have u1.n32bits ??
>
> 1. You are not allowed to write to one field of a union and read from the
> other, according to the C standard. It can be made to work with most
> compilers though if some care is taken.
> 2. Bitfields are only packed up to the size of their base type. Therefore,
> you had better use the same for all if you want them packed together.
> 3. Assuming that 'unsigned int' is 32 bit is just stupid as there is no
> guarantee for that. Use an explicitly sized integer like uint32_t (C99) or
> UINT32 (win32).
> 4. The order in those bitfields is even dependant on the endianess!
> 5. assert(sizeof u == sizeof u.n32bits);
> 6. [C++ only] No need to create an anonymous union and then use typedef to
> assign a name to it.
>
> Uli
>
ok, I know that, this code is a SAMPLE
which shows
gcc can align this union to 1 bit, vc not

of course a can solve this problem easily (all fields in struct are 32
bits).

anyway,
thx

--
Marcin Gabryszewski
G DATA Software
www.gdata.pl

address:<FirstName><dot><Surname><at><gdata><dot><pl>

Re: bug in cl compiler ?? by Igor

Igor
Fri Oct 27 07:33:52 CDT 2006

"Marcin Gabryszewski" <look_at_footer@invalid.addres.com> wrote in
message news:ehsgji$olj$1@nemesis.news.tpi.pl
> ok, I know that, this code is a SAMPLE
> which shows
> gcc can align this union to 1 bit, vc not

Well, you claimed VC has a bug, as in a deviation from specified
behavior. The behavior of your SAMPLE is unspecified, hence it cannot be
used to demonstrate a bug in the compiler.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: bug in cl compiler ?? by Tim

Tim
Sat Oct 28 01:25:54 CDT 2006

Marcin Gabryszewski <look_at_footer@invalid.addres.com> wrote:
>
>ok, I know that, this code is a SAMPLE
>which shows gcc can align this union to 1 bit, vc not

True. However, if you think that means gcc is right and Visual C++ is
wrong, you are mistaken. Both compilers are correct here.

>of course a can solve this problem easily (all fields in struct are 32
>bits).

I think you mean, declare all of the bitfields as unsigned int. If so, you
are correct.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.