From: Arne Vajhøj on
On 14-06-2010 19:25, Tom Shelton wrote:
> 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...

But that means that BOOL in Win32 C is 4 bytes not that bool
in C# is 4 bytes.

But it may be the background for Tony's book.

Arne

From: Arne Vajhøj on
On 14-06-2010 19:08, Rudy Velthuis wrote:
> 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>

Good question.

I guess the best answer is: Tony's book !

:-)

Arne

From: Rudy Velthuis on
Tom Shelton wrote:

> > 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...

Ah, that's a different case. If you push a boolean on the stack, as a
function parameter, it will take up 4 bytes, indeed (in a 32 bit
context).

--
Rudy Velthuis http://rvelthuis.de

"Pain is certain, suffering is optional."
-- Buddha
From: Rudy Velthuis on
Arne Vajh�j wrote:

> 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.

I missed that, sorry.

--
Rudy Velthuis http://rvelthuis.de

Parkinson's Law: Work expands to fill the time available for its
completion.
From: Arne Vajhøj on
On 15-06-2010 06:41, Rudy Velthuis wrote:
> Arne Vajh�j wrote:
>> 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.
>
> I missed that, sorry.

Your main point was still a very good catch!

Arne

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