473,795 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15700
Daniel Jin <an*******@disc ussions.microso ft.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.co m>
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 (typeof(Boolean ))

Regards

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

check this:
System.Runtime .InteropService s.Marshal.SizeO f(typeof(Boolea n))


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.WriteLi ne( 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.Runtim e.InteropServic es.Marshal.Size Of(typeof(Boole an))

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.WriteLi ne( sizeof(bool) ); }

Mattias


I think You're right...

I tried this:

unsafe { Console.WriteLi ne( sizeof(Char) ); }
Console.WriteLi ne(System.Runti me.InteropServi ces.Marshal.Siz eOf(typeof(Char ))
);

Regards

Marcin
Nov 16 '05 #6

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

Bool size is 4 Bytes.

check this:
System.Runtim e.InteropServic es.Marshal.Size Of(typeof(Boole an))

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.WriteLi ne( 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.WriteLi ne( sizeof(Char) ); }
Console.WriteLi ne(System.Runti me.InteropServi ces.Marshal.Siz eOf(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.o rg> wrote in message
news:#U******** *****@tk2msftng p13.phx.gbl...
Marcin,
Bool size is 4 Bytes.

check this:
System.Runtime .InteropService s.Marshal.SizeO f(typeof(Boolea n))


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.WriteLi ne( 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*******@taxu ssi.no.com.spam .pl> skrev i meddelandet
news:c5******** **@nemesis.news .tpi.pl...
Hi Mattias,

Marcin,

Bool size is 4 Bytes.

check this:
System.Runt ime.InteropServ ices.Marshal.Si zeOf(typeof(Boo lean))
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.WriteLi ne( 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*******@taxu ssi.no.com.spam .pl> skrev i meddelandet
news:c5******** **@atlantis.new s.tpi.pl...
Hi Andreas,
"Marcin GrzÄTbski" <mg*******@taxu ssi.no.com.spam .pl> skrev i meddelandet news:c5******** **@nemesis.news .tpi.pl...
Hi Mattias,
Marcin,

>Bool size is 4 Bytes.
>
>check this:
>System.Runt ime.InteropServ ices.Marshal.Si zeOf(typeof(Boo lean))
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.WriteLi ne( 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
4556
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 go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I write my functions. It seems to me sometimes that I shouldn't have many void functions accepting...
25
7597
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() might take linear time on some list implementations. That makes sense at first. However, he also says this at the very beginning: "That being the case , you might wonder why one construct should be preferred to the other, especially in view of the...
6
2183
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 were very strange and unexpected. Here is an example. Applogies for not being able to post the exact code. Struct B{ long b1;
13
471
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
3395
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 addressable unit is an int. All rules still apply for todays machines, right? Thanks in advance.
2
3613
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 RasConstatus? Thanks in Advanced. RasGetConnectStatus returning 632 error code. internal class RASCONNSTATUS
3
2742
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. Cheers Costantinos
5
1830
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 system that would be. double int
11
2425
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, that's way too much... I was wondering if the following technique is "frowned" upon: <pseudo-code> _____________ typedef struct ... vztimespec_t;
10
24822
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 got 4 I am sure the array has hundreds of bytes
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.