?
=?iso-8859-1?q?Lars_Rune_N=F8stdal?=
struct X {
unsigned int a : 1;
unsigned int b : 1;
};
int main()
{
X* x = new X;
x->a = 1;
x->b = 1;
return(0);
}
Will this code not result in the two bits 'a' and 'b', both with the value
1, following each other (packed/aligned) in memory?
Like this, where the content between [ and ] represents a single bit in
memory: ..[1][1]..
From TC++PL. (§C8.1): "..using (bit)fields to pack several variables into
a byte.."
I assume a byte (or allocation unit, or word) on any platform known to man
has room for at least two bits, and therefore will not straddle.
From http://www.parashift.com/c++-faq-lite/intrinsic-types.html: "The C++
language guarantees a byte must always have at least 8 bits."
From the C++ standard (2003) (§9.6): "Bit fields are packed into some
addressable allocation unit."
Note that I'm not taking into account the details about stuff like MSB or
the actual size of what's allocated and possibly padded, or ignored as a
value here. What I want to know is if it's guaranteed that 'a' and 'b' is
packed/aligned here.
This is the output of the GDB under Linux (x86) when examining the
contents of 'x':
14 x->a = 1;
(gdb) x x
0x804a050: 00000000000000000000000000000000
(gdb) step
15 x->b = 1;
(gdb) x x
0x804a050: 00000000000000000000000000000001
(gdb) step
16 return(0);
(gdb) x x
0x804a050: 00000000000000000000000000000011
(gdb)
Sorry if the question is wierd -- but we're having quite a discussion
about this and I need to be 100% sure about how this works (and doesn't
work).
unsigned int a : 1;
unsigned int b : 1;
};
int main()
{
X* x = new X;
x->a = 1;
x->b = 1;
return(0);
}
Will this code not result in the two bits 'a' and 'b', both with the value
1, following each other (packed/aligned) in memory?
Like this, where the content between [ and ] represents a single bit in
memory: ..[1][1]..
From TC++PL. (§C8.1): "..using (bit)fields to pack several variables into
a byte.."
I assume a byte (or allocation unit, or word) on any platform known to man
has room for at least two bits, and therefore will not straddle.
From http://www.parashift.com/c++-faq-lite/intrinsic-types.html: "The C++
language guarantees a byte must always have at least 8 bits."
From the C++ standard (2003) (§9.6): "Bit fields are packed into some
addressable allocation unit."
Note that I'm not taking into account the details about stuff like MSB or
the actual size of what's allocated and possibly padded, or ignored as a
value here. What I want to know is if it's guaranteed that 'a' and 'b' is
packed/aligned here.
This is the output of the GDB under Linux (x86) when examining the
contents of 'x':
14 x->a = 1;
(gdb) x x
0x804a050: 00000000000000000000000000000000
(gdb) step
15 x->b = 1;
(gdb) x x
0x804a050: 00000000000000000000000000000001
(gdb) step
16 return(0);
(gdb) x x
0x804a050: 00000000000000000000000000000011
(gdb)
Sorry if the question is wierd -- but we're having quite a discussion
about this and I need to be 100% sure about how this works (and doesn't
work).