469,294 Members | 1,568 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,294 developers. It's quick & easy.

size of a *bool*

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?
Nov 16 '05 #1
10 14620
Daniel Jin <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Daniel,
unless if it's in a array, then it will be 2 bytes. is that true?


No, bool is always 1 byte.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
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(type of(Boolean))

Regards

Marcin
Nov 16 '05 #4
Marcin,
Bool size is 4 Bytes.

check this:
System.Runtime.InteropServices.Marshal.SizeOf(typ eof(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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5
Hi Mattias,
Marcin,

Bool size is 4 Bytes.

check this:
System.Runtime.InteropServices.Marshal.SizeOf(ty peof(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


I think You're right...

I tried this:

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

Regards

Marcin
Nov 16 '05 #6

"Marcin GrzÄTbski" <mg*******@taxussi.no.com.spam.pl> skrev i meddelandet
news:c5**********@nemesis.news.tpi.pl...
Hi Mattias,
Marcin,

Bool size is 4 Bytes.

check this:
System.Runtime.InteropServices.Marshal.SizeOf(ty peof(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


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.
I tried this:

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

Regards

Marcin

Nov 16 '05 #7
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!

--
William Stacey, MVP

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:#U*************@tk2msftngp13.phx.gbl...
Marcin,
Bool size is 4 Bytes.

check this:
System.Runtime.InteropServices.Marshal.SizeOf(typ eof(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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 16 '05 #8
Hi Andreas,
"Marcin GrzÄTbski" <mg*******@taxussi.no.com.spam.pl> skrev i meddelandet
news:c5**********@nemesis.news.tpi.pl...
Hi Mattias,

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


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.


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

Marcin
Nov 16 '05 #9
Marcin,

"Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> skrev i meddelandet
news:c5**********@atlantis.news.tpi.pl...
Hi Andreas,
"Marcin GrzÄTbski" <mg*******@taxussi.no.com.spam.pl> skrev i meddelandet news:c5**********@nemesis.news.tpi.pl...
Hi Mattias,
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

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.


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
Marcin

Nov 16 '05 #10
William Stacey [MVP] <st***********@mvps.org> wrote:
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[i]=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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Matthias | last post: by
6 posts views Thread by MSReddy | last post: by
13 posts views Thread by Daniel Jin | last post: by
17 posts views Thread by TheMadHatter | last post: by
2 posts views Thread by Dennis Sia via DotNetMonster.com | last post: by
3 posts views Thread by costantinos | last post: by
5 posts views Thread by Gary Wessle | last post: by
11 posts views Thread by Chris Thomasson | last post: by
10 posts views Thread by Chunekit Pong | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.