From: Rudy Velthuis on
Arne Vajh�j wrote:

> On 28-05-2010 03:34, Tony Johansson wrote:
> > Is it anyone that might have a good explanation why the designor of
> > .NET made a bool 4 bytes.

Sorry, I can't see the orginal message, but (at least on my Windows
32bit) sizeof(bool) is 1, so I wonder what this discussion is about. <g>

--
Rudy Velthuis http://rvelthuis.de

"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
-- Larry Wall
From: Rudy Velthuis on
Patrice wrote:

> Hello,
>
> > 1 byte incl. padding 4 bytes incl. padding
> > 1 boolean 4 bytes 4 bytes
> > 4 boolean 4 bytes 16 bytes
>
> But then those booleans are not all aligned any more

Natural alignment means that a type is aligned on a multiple of its own
size (in bytes), so bytes are always naturally aligned, by definition.

--
Rudy Velthuis http://rvelthuis.de

"Too many pieces of music finish too long after the end."
-- Igor Stravinsky (1882-1971)
From: Rudy Velthuis on
Arne Vajh�j wrote:

> On 28-05-2010 03:34, Tony Johansson wrote:
> > Is it anyone that might have a good explanation why the designor of
> > .NET made a bool 4 bytes.
> > I mean it's just a wast of memory.
>
> Somebody at Microsoft made a decision.

Er... I just checked, and

Console.WriteLine(sizeof(bool));

printed 1 for me. On 32 bit Windows. I don't quite understand what the
fuss is all about. <g>

Of course, if the bool is part of an aligned struct, the padding bytes
may make the offset of the next member (say, an Int32)
<offset of boolean> + 4. But a bool itself is only 1 byte in size,
AFAICT. If the next member is a double, the padding can even be 7
bytes, but that does not make the bool 8 bytes in size.

IOW, this probably has a size of 16 bytes:

struct Foo
{
bool b;
double d;
}

--
Rudy Velthuis http://rvelthuis.de

"A narcissist is someone better looking than you are."
- Gore Vidal
From: Tom Shelton on
Rudy Velthuis wrote on 6/14/2010 :
> Arne Vajh�j wrote:
>
>> On 28-05-2010 03:34, Tony Johansson wrote:
>>> Is it anyone that might have a good explanation why the designor of
>>> .NET made a bool 4 bytes.
>>> I mean it's just a wast of memory.
>>
>> Somebody at Microsoft made a decision.
>
> Er... I just checked, and
>
> Console.WriteLine(sizeof(bool));
>
> printed 1 for me. On 32 bit Windows. I don't quite understand what the
> fuss is all about. <g>
>
> Of course, if the bool is part of an aligned struct, the padding bytes
> may make the offset of the next member (say, an Int32)
> <offset of boolean> + 4. But a bool itself is only 1 byte in size,
> AFAICT. If the next member is a double, the padding can even be 7
> bytes, but that does not make the bool 8 bytes in size.
>
> IOW, this probably has a size of 16 bytes:
>
> struct Foo
> {
> bool b;
> double d;
> }

sizeof represents the .net runtime size. It will return 1.
Marshal.SizeOf (typeof(bool)) will return 4, as the marshaller converts
a bool to 4 bytes when passed to native code...

--
Tom Shelton


From: Arne Vajhøj on
On 14-06-2010 19:11, Rudy Velthuis wrote:
> Patrice wrote:
>>> 1 byte incl. padding 4 bytes incl. padding
>>> 1 boolean 4 bytes 4 bytes
>>> 4 boolean 4 bytes 16 bytes
>>
>> But then those booleans are not all aligned any more
>
> Natural alignment means that a type is aligned on a multiple of its own
> size (in bytes), so bytes are always naturally aligned, by definition.

We already covered that part a week ago.

Arne

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Using Delegates?
Next: Drag & Drop text