In article <11************ **********@p79g 2000cwp.googleg roups.com>,
sarathy <sp*********@gm ail.comwrote:
>1. How it that the results for the size of struct1 and struct2 (below)
are 4 and 3
>struct struct1
{
const :16;
volatile :4;
};
>struct struct2
{
int :1;
unsigned :1;
const :16;
volatile :4;
};
With SGI's IRIX cc compiler, both are 3. With gcc 3.3 on the same
machine, the first is 4 and the second is 3. Which is to say that the
alignment mechanisms are implementation dependant, and are not required
to be consistant.
It is, for example, entirely legal for the compiler to examine the
first structure, note that the first field is the same size as a short
int on that implementation, and decide that the structure shall be
built for fast access as a pair of shorts aligned on a short boundary,
for a total of 4 bytes.
The same compiler could look at the second structure and decide that it
is complicated enough that space is the important factor rather than
speed, and decide to pull bits out of 3 bytes.
But compilers are not required to allow bitfields to cross word
boundaries, so the compiler -could- have decided to put the first two
fields into one short, the third field into a second short, and the
fourth field into a third short, for a total of 6 bytes. Or it could
have decided that since the total fits within 32 bits that it would
pull bits out of a long, for a total of 4 bytes.
Decisions about when to move to the next word and how much padding
to use before that word are completely up to the compiler: the
closest that the C standard comes on this point is to say "if it fits".
>2. Also what is meant by "incomplete type" in C?
The C faq probably talks about that in better words than I could
come up with in a reasonable time.
>3. What does tag refer to in a struct/union declaration?
A structure tag is a programmer-given name for that variety of
structure. It is not a variable, but rather a reference to the type.
As a rough analogy: "Form 37/J" might be a name given to a particular
layout of income tax form, but "Form 37/J" is not a particular -copy-
of the form, it is the name of the -kind- of form.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest