Re: padding?
- From: Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx>
- Date: 10 Jul 2009 22:52:32 -0700
Nick Keighley <nick_keighley_nospam@xxxxxxxxxxx> writes:
On 9 July, 11:30, Tim Rentsch <t...@xxxxxxxxxxxxxxxxxxx> wrote:
Nick Keighley <nick_keighley_nos...@xxxxxxxxxxx> writes:g
On 25 June, 18:20, Tim Rentsch <t...@xxxxxxxxxxxxxxxxxxx> wrote:
ralt...@xxxxxxxxx (Richard Bos) writes:
Tim Rentsch <t...@xxxxxxxxxxxxxxxxxxx> wrote:
ralt...@xxxxxxxxx (Richard Bos) writes:
What's more, padding is never a data member at all. The definin=
e itcharacteristic of padding is that it exists because of the spac=
Any datatakes, not because of the value it may or may even never have. =
at somemember, even a data member only few people find useful, should =
to.point in its life have a value that those people want to refer =
ver itPadding may change randomly or never at all, and you can blot o=
dwithout affecting anyone.
In
struct s {
unsigned foo : 4;
unsigned : 12;
unsigned bas : 16;
};
would you say the data member between foo and bas is there as
padding?
I might, if say I was describing the format of a data packet.
I don't consider it "padding" in the C sense of the word.
Since you can't access its value at all (stupid tricks with unsigne=
dchar pointers aside), and that value is therefore irrelevant, yes.
I think the "stupid trick" with unsigned char can't be ignored.
C trivia question: Is it possible to access the value of an unname=
bit-field in a way that's portable and not implementation dependent?
no. Its very position is implementation defined
Answer: of course there are implementation dependent ways (besides
using unsigned chars, there are other kinds of type punning, or
reading/writing a binary file), but it may surprise some people to
know that there is also a portable way to do so:
surprise me
struct s {
unsigned foo : 4;
unsigned : 12;
unsigned bas : 16;
};
struct t {
unsigned foo : 4;
unsigned hidden : 12;
unsigned bas : 16;
};
union s_and_t {
struct s s;
struct t t;
} s_and_t;
/* now using s_and_t.t.hidden will access */
/* the unnamed bit-field in s_and_t.s */
...and invoke Undefined Behaviour. You've replaced IB with UB.
No, this is defined behavior, not undefined behavior.
Please see 6.5.2.3 p 5
what's the section titled, "Structure and Union Members"?
Or could you quote it? I don't have an ISO standard handy.
This is the "common initial sequence" bit? Interesting.
Yes, the common initial sequence guarantee. Here is the text of
paragraph 5:
One special guarantee is made in order to simplify the use
of unions: if a union contains several structures that
share a common initial sequence (see below), and if the
union object currently contains one of these structures, it
is permitted to inspect the common initial part of any of
them anywhere that a declaration of the complete type of the
union is visible. Two structures share a common initial
sequence if corresponding members have compatible types
(and, for bit-fields, the same widths) for a sequence of one
or more initial members.
.
- References:
- Re: padding?
- From: Nick Keighley
- Re: padding?
- From: Tim Rentsch
- Re: padding?
- From: Nick Keighley
- Re: padding?
- Prev by Date: how real and integer values are saved in C language
- Next by Date: Re: linking c++ code with c library
- Previous by thread: Re: padding?
- Next by thread: Re: My code to determine whether two words are anagrams won't work.
- Index(es):
Relevant Pages
|