-
Mar 23rd, 2015, 05:50 AM
#1
Thread Starter
Hyperactive Member
Memory allocation: Byte Vs Boolean
What is the difference between Memory allocation of datatype Byte and Boolean?
-
Mar 23rd, 2015, 05:52 AM
#2
Re: Memory allocation: Byte Vs Boolean
Have you not read both links?
-
Mar 23rd, 2015, 06:46 AM
#3
Re: Memory allocation: Byte Vs Boolean
If you read far enough through both links, and sub links, you will see in .Net that both are structures to support the methods associated with them, and in memory the storage for each data element is a byte.
So, memory allocation wise, they should be the same, taking one byte per element in another class or structure.
Of course, in use, they should be treated completely differently.
-
Mar 23rd, 2015, 07:37 AM
#4
Re: Memory allocation: Byte Vs Boolean
Unless interfacing with a legacy application, or automation/industrial system, does it matter?
If interfacing, then rather than relying on the underlying representation of a boolean - as that's the only one that matters, here, since a byte is a byte - then you'd explicitly layout the representation. For example, a byte can represent 8 booleans, or a single boolean; if a single boolean is it 1, not zero or all 1's for true?
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Mar 23rd, 2015, 08:19 AM
#5
Re: Memory allocation: Byte Vs Boolean
And if interfacing to VB6, a boolean is two bytes (based on the VB6 integer type).
I'm assuming the reason for the question about .Net was because a byte and a boolean were different sizes in VB6.
If a boolean was still two bytes (or more), one might choose to manually implement "booleans", by using a smaller type, a byte, or individual bits within a larger integral type, e.g. byte, short, integer, etc.
-
Mar 23rd, 2015, 12:03 PM
#6
Re: Memory allocation: Byte Vs Boolean
I have my doubts as to whether .NET truly uses a byte for either byte or Boolean, despite what the documentation states.
My usual boring signature: Nothing
-
Mar 23rd, 2015, 01:02 PM
#7
Re: Memory allocation: Byte Vs Boolean
Why not? We're only talking about the data portion of the structure.
All the methods that pertain to the structure are a separate thing, and are shared by all objects of that structure type.
When you call a method on the object, e.g. myBoolean.ToSTring, the ToString method of the Boolean structure is called, probably passing the byte by value that is the boolean associated with myBoolean variable to it.
There is no reason that all the places you declare booleans or bytes can't be single bytes, as it is the compiler that generates the code that knows that byte is of type Boolean or Byte structure and can call the methods associated with that type passing the byte to them.
Even if you choose to pass the byte or boolean by reference, then the compiler would pass the address of the byte where the boolean or byte is located. Passing the address of a byte doesn't prevent the byte from being a byte itself.
-
Mar 24th, 2015, 03:26 AM
#8
Re: Memory allocation: Byte Vs Boolean
Originally Posted by passel
If you read far enough through both links, and sub links, you will see in .Net that both are structures to support the methods associated with them, and in memory the storage for each data element is a byte.
So, memory allocation wise, they should be the same, taking one byte per element in another class or structure.
Of course, in use, they should be treated completely differently.
A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
-
Mar 24th, 2015, 07:51 AM
#9
Re: Memory allocation: Byte Vs Boolean
Originally Posted by Niya
A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
is this also true for a real 64bit app?
once upon a time it was 16bits
but as someone else said: does it really matter? if you got a huge amount of booleans to save (e.g. i had to count the number of unique colors in a 32bit rgb picture once) you are better served with manually setting bits i.e. 1 bit per boolean.
-
Mar 24th, 2015, 09:41 AM
#10
Thread Starter
Hyperactive Member
Re: Memory allocation: Byte Vs Boolean
Originally Posted by Niya
A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
If Boolean takes 4 byte memory then we can use Byte instead of Boolean in some cases(0 or 1). Is that so?
-
Mar 24th, 2015, 10:14 AM
#11
Re: Memory allocation: Byte Vs Boolean
What is it you are trying to do?
Are you creating a large array of Booleans and trying to save memory by using a Type that has smaller memory consumption?
If so, I wouldn't bother using a Byte array to hold each Boolean value, since Booleans are definitely stored as Bytes when stored in an array (and consequently in a List).
Don't know about individual Boolean variables, but I suspect their values are stored as a Byte, but word aligned to 4 bytes (so you could store up to 4 Booleans in a 4 byte word).
-
Mar 24th, 2015, 10:17 AM
#12
Re: Memory allocation: Byte Vs Boolean
For what purpose? The registers in the CPU are either 32 or 64 bits. Memory can be addressed down to the byte, in theory, but in practice it is terribly inefficient to move around a byte at a time, so that just doesn't happen even if the data type is a byte in size. Data structures may also be stored aligned to certain boundaries to improve efficiency, which means that the address is evenly divisible by some number like 4, 8, or 16. So, the memory is likely not stored at the one byte size, moved around at the one byte size, or used in the CPU registers at one byte size, so what do you expect to gain by using a byte over a Boolean?
My usual boring signature: Nothing
-
Mar 24th, 2015, 10:37 AM
#13
Re: Memory allocation: Byte Vs Boolean
Originally Posted by Niya
A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
Marshal.SizeOf is the size of the object when allocated to unmanaged memory. That is not necessarily the size the object is in managed memory.
My understanding is that in the CLI, the boolean is only 1 byte. Compiled, the size varies.
I'm also given the impression if you have a boolean array with a size of 16 elements, and you use Marshal.Copy to copy it to unmanaged memory, the size of the array is 16 bytes.
Unfortunately, I don't have time to play with any of this to verify the various cases. One byte in CLI may not translate to 1 byte in the compiled executable, which is probably part of the confusion.
Of course, you also have available the BitArray class so you can have compact storage, if that is a requirement.
Haven't done any speed tests between the representations to measure the tradeoffs.
-
Mar 24th, 2015, 05:48 PM
#14
Re: Memory allocation: Byte Vs Boolean
Originally Posted by passel
Marshal.SizeOf is the size of the object when allocated to unmanaged memory. That is not necessarily the size the object is in managed memory.
Ideally yes, but in practice, I've observed no discrepancies with a primitive type's managed and unmanaged sizes. It returns 1 for Bytes, 2 for Shorts, 4 for Integers and 8 for Longs. I see no reason why the Boolean type would be a special case. Even in VB6 a Boolean was simply a Short(Integer in VB6).
-
Mar 24th, 2015, 05:55 PM
#15
Re: Memory allocation: Byte Vs Boolean
Also, the documentation states that a Boolean's storage size is platform specific. In other words, you have no guarantees that its 1 byte or 4 bytes. It happens to be 4 bytes in Windows.
-
Mar 24th, 2015, 06:09 PM
#16
Re: Memory allocation: Byte Vs Boolean
I decided to dig a little more into this and I found a blog post by JaredPar who works at MS. He confirmed that you are indeed right passel. A Boolean is 1 byte as a managed type and its default marshalling behavior is to be sent as 4 bytes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|