473,761 Members | 5,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

smallest type

Bob
Hi,
I have a newbie question. What is the smallest type object size? I am
doing some calculation with individual bits and want to minimize memory
usage. I though bool might do it but it seems like each bool is a
byte, instead of a bit. Is bool the smallest possible type?

Thanks,
Bob

Jan 25 '07 #1
13 9696
Hi Bob,

Normally byte is used as the smallest integral data type.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Bob" <bs********@yah oo.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hi,
I have a newbie question. What is the smallest type object size? I am
doing some calculation with individual bits and want to minimize memory
usage. I though bool might do it but it seems like each bool is a
byte, instead of a bit. Is bool the smallest possible type?

Thanks,
Bob

Jan 25 '07 #2
Bob wrote:
I have a newbie question. What is the smallest type object size? I am
doing some calculation with individual bits and want to minimize memory
usage. I though bool might do it but it seems like each bool is a
byte, instead of a bit. Is bool the smallest possible type?
If speed is important use byte else you can try the
System.Collecti ons.BitArray class.

Arne
Jan 25 '07 #3
In that case, BitVector32 is another alternative if you need to use flags or
small integers, "but it has the memory and performance overhead that a class
instance requires. In contrast [to BitArray], a BitVector32 uses only 32
bits" [1].

[1] BitVector32 Structure
http://msdn2.microsoft.com/en-us/lib...tvector32.aspx

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
Bob wrote:
>I have a newbie question. What is the smallest type object size? I am
doing some calculation with individual bits and want to minimize memory
usage. I though bool might do it but it seems like each bool is a
byte, instead of a bit. Is bool the smallest possible type?

If speed is important use byte else you can try the
System.Collecti ons.BitArray class.

Arne

Jan 25 '07 #4
Bob
Thanks for all the quick replies. By use byte, do you mean that an
byte[] where all the entries are either 0 or 1 will perform faster than
a similarly size bool[]?

Thanks,
Bob

On Jan 24, 9:27 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
Bob wrote:
I have a newbie question. What is the smallest type object size? I am
doing some calculation with individual bits and want to minimize memory
usage. I though bool might do it but it seems like each bool is a
byte, instead of a bit. Is bool the smallest possible type?If speed isimportant use byte else you can try the
System.Collecti ons.BitArray class.

Arne
Jan 25 '07 #5
>By use byte, do you mean that an
>byte[] where all the entries are either 0 or 1 will perform faster than
a similarly size bool[]?
I think they will perform pretty much the same, since bytes and bools
are usually treated the same way by the runtime. Both types are 1
byte.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 25 '07 #6
"Bob" <bs********@yah oo.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
Hi,
I have a newbie question. What is the smallest type object size? I am
doing some calculation with individual bits and want to minimize memory
usage. I though bool might do it but it seems like each bool is a
byte, instead of a bit. Is bool the smallest possible type?

Thanks,
Bob

It depends on what you mean with "object" size, the smallest *Reference type* size type is
an int.
byte b = 1;
object o = b;
here, o will take 12 bytes (on X86), 8 bytes object header plus 4 bytes for the byte value.

If you mean primitive type size, then by definition the smallest type is a byte, but again
it highly depends on where and how they are allocated.

void Foo()
{
byte b;
int i;
...

In above sample, both b and i will be stored on the stack, both i and b will occupy a slot
on the stack( Int32 on x86).
void Foo()
{
byte b;
int i;
byte b2;
byte b3,
byte b4;

In above, the four bytes will occupy a single slot (4 bytes on X86) just like i. so here the
same amount of memory is used as the previous sample.

class Foo{
byte b;
int i;
byte b2;
byte b3,
byte b4;
}

class Bar {
byte b;
int i;
}

Same here, An instance of Foo or Bar will occupy the same amount of memory, that is, 8
bytes header plus 8 bytes for the data members.
Conclusion, it makes little sense to use a smaller value than an int if you can't combine a
number of these small integer values.

Willy.

Jan 25 '07 #7
"Mattias Sjögren" <ma************ ********@mvps.o rgha scritto nel messaggio
I think they will perform pretty much the same, since bytes and bools
are usually treated the same way by the runtime. Both types are 1
byte.
mmm... I know that in .net a bool is a 4 bytes, and used as a Int32 in
compiled IL for compares.
And for performances a 4 bytes on a 32bits architecture is best over each
other lengths.
If the aim is a small flags data type use an enum x : byte

Personally I will avoid these thoughts about memory allocation in .Net, sice
it is fully managed by the famework and the garbage collector (also the data
in the taskmanager about .net apps are all lies).
Jan 25 '07 #8
>mmm... I know that in .net a bool is a 4 bytes
You do eh? And yet Console.WriteLi ne(sizeof(bool) ) prints 1.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 25 '07 #9
"Mattias Sjögren" <ma************ ********@mvps.o rgha scritto nel messaggio
>
>>mmm... I know that in .net a bool is a 4 bytes

You do eh? And yet Console.WriteLi ne(sizeof(bool) ) prints 1.
And my IL says that bool x = true is

L_0001: ldc.i4.1

i4 stays for Int32.
1 stays for the "true" value (0 is false).

And now? ;)
Jan 25 '07 #10

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

Similar topics

4
6487
by: Code4u | last post by:
I need to write an algorithm that sheds the outliers in a large data set, for example, I might want to ignore the smallest 2% of values and find the next smallest. Boost has a nth_element algorithm, however, it partially sorts the data. I have a requirement that the data remain in the orginal order. Of course I could make a copy of the vector or array and then apply nth_element, but this would be expensive in memory and would require...
2
2273
by: J. J. Cale | last post by:
In IE6 both these functions *seem* to be working. I don't see much recursion in this group except the occasional setTimeout problems. Besides the obvious stack problems that can occur if one recurses too deep are there other reasons for this that I should know about? This example returns the position of the leftmost element of a specific type but it could be any array or any type of comparison regex or whatever. Also I'm sure these can...
13
5155
by: Peter Ammon | last post by:
I have a floating point number. I'd like to get the nearest floating point number that is larger or smaller than the given number. I investigated FLT_EPSILON but it only seems to be useful if the given number is 1. Any suggestions? Thanks, -Peter
11
13125
by: gouqizi.lvcha | last post by:
Hi, All: I wonder what is the smallest positive double numbers in C in 32 bit CPU? Rick
22
7739
by: CK | last post by:
Hi EveryBody! This is the World Smallest Program in c. main(l ,a,n,d)char**a;{ for(d=atoi(a)/10*80- atoi(a)/5-596;n="@NKA\ CLCCGZAAQBEAADAFaISADJABBA^\ SNLGAQABDAXIMBAACTBATAHDBAN\ ZcEMMCCCCAAhEIJFAEAAABAfHJE\
4
9830
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
6
4435
by: yokaygirl143 | last post by:
i am writing a simple java program but everything i do it seems to not work. i need to find the smallest and second smallest number in a set. please help me understand what i have done wrong. heres what i figured(but doesn't work fully): public class TwoSmallest { public static void main(String args) { int size = IO.readInt(); if (size <= 0)
8
3470
by: Sunny | last post by:
Hi, do someone know, How we can find the smallest distance between a bunch of lat 7 long? Like I have 10 Latitude & Longitude. -73.924598,40.879010 -73.924506,40.878978 -73.924506,40.878978 -73.921406,40.878178 -73.921406,40.878178
8
12184
crystal2005
by: crystal2005 | last post by:
I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me Segmentation fault (core dumped) :(( It looks simple but i have no idea what went wrong.... Can anyone help me out?? #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_INPUT 25
0
9377
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
9989
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
9811
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...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
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
5266
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
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.