473,382 Members | 1,357 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,382 software developers and data experts.

sizeof(bool) quick question

Hi there,

i want to know size of managed bool (know it is 1byte according to language
spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?

Thanks in advance

Brian
Nov 17 '05 #1
6 4299
You can use sizeof(bool) but then you need to compile with the /unsafe
switch
and also place that unsafe code in an unsafe code block.

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

PS
Specifying to compile with the unsafe switch can also be done from the
VS.NET IDE
under Project->[ProjectName] Properties...->Configuration Properties->Build

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Brian Keating" <Br**********@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
Hi there,

i want to know size of managed bool (know it is 1byte according to
language
spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?

Thanks in advance

Brian

Nov 17 '05 #2
Brian Keating wrote:
Hi there,

i want to know size of managed bool (know it is 1byte according to
language spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?


You can use sizeof in C# but you have to specify the code as unsafe. Ex:

unsafe static int GetSize()
{
return sizeof(bool);
}

You will see that returns 1.
--
Tom Porterfield
Nov 17 '05 #3
Thanks for your reply.
"Dennis Myrén" wrote:
You can use sizeof(bool) but then you need to compile with the /unsafe
switch
and also place that unsafe code in an unsafe code block.

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

PS
Specifying to compile with the unsafe switch can also be done from the
VS.NET IDE
under Project->[ProjectName] Properties...->Configuration Properties->Build

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Brian Keating" <Br**********@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
Hi there,

i want to know size of managed bool (know it is 1byte according to
language
spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?

Thanks in advance

Brian


Nov 17 '05 #4
Thanks for your reply

"Tom Porterfield" wrote:
Brian Keating wrote:
Hi there,

i want to know size of managed bool (know it is 1byte according to
language spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?


You can use sizeof in C# but you have to specify the code as unsafe. Ex:

unsafe static int GetSize()
{
return sizeof(bool);
}

You will see that returns 1.
--
Tom Porterfield

Nov 17 '05 #5
Brian,

FWIW, sizeof(bool) can be used without unsafe in C# v2.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #6
Excellent!
I've moved to v2 as of last week.

Thanks a mill

Brian Keating
bfa Ireland

"Mattias Sjögren" wrote:
Brian,

FWIW, sizeof(bool) can be used without unsafe in C# v2.

Mattias

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

Nov 17 '05 #7

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

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
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?
7
by: yashwant pinge | last post by:
I have written a small main function . I have defined one bool variable and cout this variable , I found the value of this bool variable is 64. Since, I am using 32-bit addressing . My...
13
by: a2z | last post by:
Hi all, Can someone throw some light on the implemetation of bool data type in C++? I have heard that it is bit based, but want to confirm this information. Is there any source code where I can...
11
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
32
by: Simon L | last post by:
BOOL bMyarray; memset(bMyArray , 0xFF, sizeof(BOOL) * 1000); clean & quick until someone else's code found I was returning -1 to mean true Because my BOOL is 4 bytes long, TRUE in memory...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.