size of a *bool*

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I read somewhere that a bool is 1 byte unless if it's in a array, then it will be 2 bytes. is that true? if so, any explaination to why that is?
 
Daniel Jin said:
I read somewhere that a bool is 1 byte unless if it's in a array,
then it will be 2 bytes. is that true? if so, any explaination to why
that is?

No, I don't believe that's true. I'm 99.9% sure that it's a byte either
way. If it's boxed it'll be a lot more, obviously.
 
Hi Daniel,
I read somewhere that a bool is 1 byte unless if it's in a array, then it will be 2 bytes. is that true? if so, any explaination to why that is?

Bool size is 4 Bytes.

check this:
System.Runtime.InteropServices.Marshal.SizeOf(typeof(Boolean))

Regards

Marcin
 
Marcin,
Bool size is 4 Bytes.

check this:
System.Runtime.InteropServices.Marshal.SizeOf(typeof(Boolean))

No, what you're getting here is the size of a bool when marshaled to a
native Win32 BOOL, which is indeed four bytes.

Try this instead

unsafe { Console.WriteLine( sizeof(bool) ); }



Mattias
 
Hi Mattias,
Marcin,




No, what you're getting here is the size of a bool when marshaled to a
native Win32 BOOL, which is indeed four bytes.

Try this instead

unsafe { Console.WriteLine( sizeof(bool) ); }

Mattias

I think You're right...

I tried this:

unsafe { Console.WriteLine( sizeof(Char) ); }
Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Char))
);

Regards

Marcin
 
Marcin GrzÄTbski said:
Hi Mattias,


I think You're right...

I would go as far as to claim he is right. The managed and unmanaged
world is completly different and the Marshal.SizeOf lives in the unmanaged
realm.
 
Yeh. However is it not true that because of alignment, the smallest thing
you can allocate is a 4 byte int. So I "think", internally, it is an int.
Not sure, but thought I came across this once. Please correct if in error
here. Cheers!
 
Hi Andreas,
I would go as far as to claim he is right. The managed and unmanaged
world is completly different and the Marshal.SizeOf lives in the unmanaged
realm.

hmmm...
Could you tell me if there is any *sizeof* method that works with
managed value types (e.g. System.Drawing.Color) ?

Marcin
 
Marcin,

Marcin Grzêbski said:
Hi Andreas,


hmmm...
Could you tell me if there is any *sizeof* method that works with
managed value types (e.g. System.Drawing.Color) ?

The sizeof operator is used to retrieve the size of a valuetype.

//Andreas
 
William Stacey said:
Yeh. However is it not true that because of alignment, the smallest thing
you can allocate is a 4 byte int. So I "think", internally, it is an int.
Not sure, but thought I came across this once. Please correct if in error
here. Cheers!

Well, I would certainly hope that an array of bools (or bytes) wouldn't
end up being an array of ints - everything which had to load an array
of bytes would end up being 4 times as large as it needed to be!

Bytes and bools at least are also packed, rather than aligned. This
program demonstrates that for bytes - just find and replace byte with
bool to see the same for bools. (They're only packed to one per byte
though, not one per bit!)

using System;

public class Test
{
byte b0;
byte b1;
byte b2;
byte b3;
byte b4;
byte b5;
byte b6;
byte b7;
byte b8;
byte b9;
byte b10;
byte b11;
byte b12;
byte b13;
byte b14;
byte b15;
byte b16;
byte b17;
byte b18;
byte b19;
byte b20;
byte b21;
byte b22;
byte b23;
byte b24;
byte b25;
byte b26;
byte b27;
byte b28;
byte b29;
byte b30;
byte b31;
byte b32;
byte b33;
byte b34;
byte b35;
byte b36;
byte b37;
byte b38;
byte b39;
byte b40;
byte b41;
byte b42;
byte b43;
byte b44;
byte b45;
byte b46;
byte b47;
byte b48;
byte b49;

static void Main()
{
Test[] ta = new Test[100000];
long orig = GC.GetTotalMemory(true);
for (int i=0; i < ta.Length; i++)
ta=new Test();
long final = GC.GetTotalMemory(true);
Console.WriteLine ("Total use={0}. Per object={1}", final-orig,
(final-orig)/ta.Length);
GC.KeepAlive(ta);
}
}

The usage is 60 bytes per object which is what I'd expect: the space
for the bytes is 50, plus 8 for the object header, rounded up to the
nearest alignment.
 
Andreas said:
Marcin,




The sizeof operator is used to retrieve the size of a valuetype.

//Andreas

I think your're right...
I found that System.Drawing.Point/PointF/Rectangle works great
with sizeof. But i found (yesterday, by accident) that type
System.Drawing.Color is immune to sizeof function.

i've got an error:
"Cannot take the address or size of a variable of a managed type
('System.Drawing.Color')"

Color type looks as a standard struct type, so what is wrong?

Is it my NET 2002 studio error or more common problem?

Marcin
 
Marcin,
Color type looks as a standard struct type, so what is wrong?

It contains a field that's a reference type (specifically a private
string member called 'name'). sizeof (and unsafe code in general) only
works on value types that are only made up of other value types.



Mattias
 
Hi Mattias,
Marcin,




It contains a field that's a reference type (specifically a private
string member called 'name'). sizeof (and unsafe code in general) only
works on value types that are only made up of other value types.



Mattias

Now i get it!
Thanx!

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top