473,416 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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 15613
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
6
by: MSReddy | last post by:
Hi Group, I have a structure than has other structures in it. I added 3 members to a strucuture and my program started behaving very strangly. When I printed sizes of the strucutures the values...
13
by: Daniel Jin | last post by:
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?
17
by: TheMadHatter | last post by:
yello, Quick Q: With regards to ram use, there really isnt any memory savings if I use type bool, vs int....... is there? If I remmeber correctly from a microcontroller course, the smallest...
2
by: Dennis Sia via DotNetMonster.com | last post by:
Hi everybody, Unable to resolve the size of RasConnStatus which causing the RasGetConnectStatus() of code # 632. Is there anyone who can tell me how to get the correct size (structure) for the...
3
by: costantinos | last post by:
Hello. I have a map map<int, vector<int and I want to find which one of the vectors has the most elements. Is there a built in function that does that or do I have to write my one code. ...
5
by: Gary Wessle | last post by:
Hi I read some where about the order variables are declared in a class may help space optimization. find the size of bits a variable takes and declare them from biggest to the lowest. so in my...
11
by: Chris Thomasson | last post by:
Consider an an object that that can has 7 or 8 functions. If you create an abstract base class for the "interface" of the object, well, that means 7 or 8 pure virtual functions right? Well, IMHO,...
10
by: Chunekit Pong | last post by:
I have a BYTE array - BYTE const* pbBinary I would like to know how many bytes in that byte array but if I do - sizeof(* pbBinary); - then I got 1 but if I do - sizeof( pbBinary); - then I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.