However, explain this !
I did four trials.
=============================
Trial 1,
main()
{
unsigned char a:1;
unsigned char b:1;
unsigned char c:1;
unsigned char d:1;
union
{
unsigned char x:3;
struct
{
unsigned char f:1;
unsigned char g:1;
unsigned char h:1;
}
}
s;
s.a = 1;
s.f = 1;
}
In this trial, s.f compiles to _s$[ebp+1]. This looks ok.
Trial 2,
main()
{
unsigned char a:1;
unsigned char b:1;
unsigned char c:1;
unsigned char d:1;
union
{
unsigned long x:3;
struct
{
unsigned long f:1;
unsigned long g:1;
unsigned long h:1;
}
}
s;
s.a = 1;
s.f = 1;
}
In this trial, s.f compiles to _s$[ebp+4]. That also looks ok.
=============================
Trial 3,
main()
{
unsigned char a:1;
unsigned char b:1;
unsigned char c:1;
unsigned char d:1;
union
{
unsigned char x:3;
struct
{
unsigned long f:1;
unsigned long g:1;
unsigned long h:1;
}
}
s;
s.a = 1;
s.f = 1;
}
In this trial, s.f compiles to _s$[ebp+4]. This is consistent with the
alignment view.
However, Trial 4,
main()
{
unsigned char a:1;
unsigned char b:1;
unsigned char c:1;
unsigned char d:1;
union
{
unsigned long x:3;
struct
{
unsigned char f:1;
unsigned char g:1;
unsigned char h:1;
}
}
s;
s.a = 1;
s.f = 1;
}
In this trial, s.f compiles to _s$[ebp+1] again ! So, I’m not too sure about
the rationale any longer. Can anyone explain what’s going on ?
Alberto.
-----Original Message-----
From: Michal Vodicka [mailto:xxxxx@veridicom.cz.nospam]
Sent: Friday, March 08, 2002 4:26 PM
To: NT Developers Interest List
Subject: [ntdev] RE: Interesting quirk of union
I agree it is alingment issue. MSDN says following (Storage and Alignment of
Structures):
Every data object has an alignment-requirement. For structures, the
requirement is the largest of its members. Every object is allocated an
offset so that
offset % alignment-requirement == 0
and
Bit fields default to size long for the Microsoft C compiler. Structure
members are aligned on the size of the type or the /Zp[n] size, whichever is
smaller. The default size is 4.
Best regards,
Michal Vodicka
STMicroelectronics Design and Application s.r.o.
[michal.vodicka@st.com, http:://www.st.com]
From:
xxxxx@compuware.com[SMTP:xxxxx@compuware.com]
Reply To: xxxxx@lists.osr.com
Sent: Friday, March 08, 2002 9:48 PM
To: xxxxx@lists.osr.com
Subject: [ntdev] RE: Interesting quirk of union
I believe the issue isn’t one of minimum size, but one of alignment.
Harbison and Steele (Section 5.7) states that “An object of a union type
will begin on a storage alignment boundary appropriate for any contained
component”. So, the union aligns on a char boundary. I went to MSVC and
replaced all “char” with “long”, and, what do you know, the union is now
at
offset +4 instead of +1. It’s a question of alignment, and it seems to be
standard C behavior.
Alberto.
-----Original Message-----
From: xxxxx@seagate.com [mailto:xxxxx@seagate.com]
Sent: Friday, March 08, 2002 3:36 PM
To: NT Developers Interest List
Subject: [ntdev] RE: Interesting quirk of union
Actually, the C++ bible is Stroustrup, and it neglects to mention the
minimum size requirement. Since I was using C++, I consider it an
oversight in the documentation.
Phil
“Gregory G. Dyess” @lists.osr.com on 03/08/2002 12:26:53
> PM
>
> Please respond to “NT Developers Interest List”
>
> Sent by: xxxxx@lists.osr.com
>
>
> To: “NT Developers Interest List”
> cc:
>
> Subject: [ntdev] RE: Interesting quirk of union
>
>
> That is correct behavior. Refer to the C/C++ bibles, name K&R. The
> minimum
> size of any structure/union/enum is 1 byte.
>
> Greg
>
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@compuware.com
> To unsubscribe send a blank email to %%email.unsub%%
>
> —
> You are currently subscribed to ntdev as: michal.vodicka@st.com
> To unsubscribe send a blank email to %%email.unsub%%
>
—
You are currently subscribed to ntdev as: xxxxx@compuware.com
To unsubscribe send a blank email to %%email.unsub%%