473,605 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable size?

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.
Nov 17 '05 #1
17 3367
TheMadHatter wrote:
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.


well I don't know that, but I would imagine that when you create a bool,
the compiler allocates a single bit, and when you create an int, the
compiler allocates 8 bits, but i honestly don't know.

what I *do* know is that it is a very good idea to use the right types
in the right places, because it makes reading your code later much
easier. "why did he use an int to hold a true or a false (0 or 1)?
idiot" is heard often in places i've worked.

Strongly typed means strongly typed. Use Perl if you don't like typed
languages.

That's just my opinion.
Nov 17 '05 #2
In .NET, a Boolean value takes up 8 bits, even though only 1 bit is really
being used.

There are ways to address a single bit at a time and can be used by either
the programmer or the runtime, quite often that involves using a larger
variable (such as a 32 bit int) as a bit field and giving a meaning to each
bit and bit-masking each off as needed for reading and writing.

Brendan

"TheMadHatt er" wrote:
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.

Nov 17 '05 #3
Also, compilers may opitmize the code. For instance, there is nothing
preventing the compiler from creating an int that represents 32 boolean
values and then just use pointer arithmetic and AND/OR operations to extract
the bit before casting it to a bool.

I don't know how the CLR is implemented, but I usually do not worry about
small details like you were asking.
"jeremiah johnson" <na*******@gmai l.com> wrote in message
news:OC******** ******@TK2MSFTN GP09.phx.gbl...
TheMadHatter wrote:
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.


well I don't know that, but I would imagine that when you create a bool,
the compiler allocates a single bit, and when you create an int, the
compiler allocates 8 bits, but i honestly don't know.

what I *do* know is that it is a very good idea to use the right types in
the right places, because it makes reading your code later much easier.
"why did he use an int to hold a true or a false (0 or 1)? idiot" is heard
often in places i've worked.

Strongly typed means strongly typed. Use Perl if you don't like typed
languages.

That's just my opinion.

Nov 17 '05 #4
"jeremiah johnson" <na*******@gmai l.com> wrote in message
news:OC******** ******@TK2MSFTN GP09.phx.gbl...
well I don't know that, but I would imagine that when you create a bool,
the compiler allocates a single bit, and when you create an int, the
compiler allocates 8 bits, but i honestly don't know.
This isn't correct. An int is stored in 32 bits and a boolean is stored as
8, 16 or 32 bits depending on the language. I think C# uses 32bits but I'm
not certain. I doubt it will use any optimisations to reduce that number of
bits, especially when that bool value is in an array.
what I *do* know is that it is a very good idea to use the right types in
the right places, because it makes reading your code later much easier.
"why did he use an int to hold a true or a false (0 or 1)? idiot" is heard
often in places i've worked.


That's true although the op may save much memory by using a byte instead of
bool if it's a large array.

Michael
Nov 17 '05 #5
> I think C# uses 32bits but I'm not certain.

No, a bool in C# (and in general a System.Boolean in any .NET language) is
one byte...
That's true although the op may save much memory by using a byte instead of
bool if it's a large array.


.... so there's no point in using byte over bool.
Mattias

Nov 17 '05 #6

"Michael C" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uQ******** *****@tk2msftng p13.phx.gbl...
"jeremiah johnson" <na*******@gmai l.com> wrote in message
news:OC******** ******@TK2MSFTN GP09.phx.gbl...
well I don't know that, but I would imagine that when you create a bool,
the compiler allocates a single bit, and when you create an int, the
compiler allocates 8 bits, but i honestly don't know.


This isn't correct. An int is stored in 32 bits and a boolean is stored as
8, 16 or 32 bits depending on the language. I think C# uses 32bits but I'm
not certain. I doubt it will use any optimisations to reduce that number
of bits, especially when that bool value is in an array.


This isn't correct also, the CLI Boolean type is 8 bits, the language has
nothing to do with this, this is the whole point of the Common Type System.
Willy.
Nov 17 '05 #7
On Mon, 3 Oct 2005 14:24:02 -0700, TheMadHatter
<Th**********@d iscussions.micr osoft.com> wrote:
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.


If you need to save memory by storing boolean values in a more compact
way use System.BitArray . It only uses 1 bit per boolean plus the usual
overhead for being a class.

Here is the size in bytes of the basic value types:

1 sbyte,byte,bool
2 short,ushort,ch ar
4 int,uint,float
8 long,ulong,doub le

The following may vary depending on Hardware/Implementation, but I
will use my computer, an ordinary PC running in 32-bit mode using .Net
2.0 Beta 2.

IntPtr,Referenc e: 4 (On a PC running in 64-bit mode it would be 8)

Object Instance: 8 + size of fields (Minimum total size of 12, Always
divideable by 4)

Struct: size of fields (Minimum of 1)

Array: 12 bytes + size of content (Total size always divideable by 4,
Much like an object instance)

Examples:

// 8+4+2+2+1 = 18 => Round up to 20 bytes per instance
class Test
{
int A; short B; char c; byte d; byte e;
}

Test[] t = new Test[1000000]; // 12 + 4 * 1 000 000 bytes (references)
for(int i=0;i<t.Length; i++)
t[i] = new Test() // 20 * 1 000 000 bytes (instances)

//For a total of 24 000 012 bytes

// 4 + 2 + 2 + 1 = 9
struct Test2
{
int A; short B; char c; byte d; byte e;
}

Test2[] = new Test2[1000000] //12 + 10 000 000 bytes

// For a total of 10 000 012 bytes

--
Marcus Andrén
Nov 17 '05 #8
Some small corrections (***).

Willy.

"Marcus Andrén" <a@b.c> wrote in message
news:o3******** *************** *********@4ax.c om...
On Mon, 3 Oct 2005 14:24:02 -0700, TheMadHatter
<Th**********@d iscussions.micr osoft.com> wrote:
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
microcontroll er
course, the smallest addressable unit is an int. All rules still apply for
todays machines, right?

Thanks in advance.
If you need to save memory by storing boolean values in a more compact
way use System.BitArray . It only uses 1 bit per boolean plus the usual
overhead for being a class.

Here is the size in bytes of the basic value types:

1 sbyte,byte,bool
2 short,ushort,ch ar
4 int,uint,float
8 long,ulong,doub le

The following may vary depending on Hardware/Implementation, but I
will use my computer, an ordinary PC running in 32-bit mode using .Net
2.0 Beta 2.

IntPtr,Referenc e: 4 (On a PC running in 64-bit mode it would be 8)

Object Instance: 8 + size of fields (Minimum total size of 12, Always
divideable by 4)

Struct: size of fields (Minimum of 1)

Array: 12 bytes + size of content (Total size always divideable by 4,
Much like an object instance)

Examples:

// 8+4+2+2+1 = 18 => Round up to 20 bytes per instance


*** 8+4+2+2+1+1 = 18 => Round up to 20 bytes per instance
class Test
{
int A; short B; char c; byte d; byte e;
}

Test[] t = new Test[1000000]; // 12 + 4 * 1 000 000 bytes (references)
for(int i=0;i<t.Length; i++)
t[i] = new Test() // 20 * 1 000 000 bytes (instances)

//For a total of 24 000 012 bytes

// 4 + 2 + 2 + 1 = 9
*** // 4 + 2 + 2 + 1 + 1 = 10
struct Test2
{
int A; short B; char c; byte d; byte e;
}

Test2[] = new Test2[1000000] //12 + 10 000 000 bytes

// For a total of 10 000 012 bytes


*** Test2 is 12 bytes in total : 4 + 2 + 2 + 1 + 1 = 10 rounded up to the
nearest multiple of int size, makes 12

So total size of Test2 array of 1000000 elements is 120012 bytes.

Note that this is valid for the current versions of the (32bit) CLR but is
in no way guaranteed, the CLR is free to map field elements as it sees fit.

Willy.
Nov 17 '05 #9
On Tue, 4 Oct 2005 13:26:23 +0200, "Willy Denoyette [MVP]"
<wi************ *@telenet.be> wrote:
Some small corrections (***).


** Cut correction of typos **
struct Test2
{
int A; short B; char c; byte d; byte e;
}

Test2[] = new Test2[1000000] //12 + 10 000 000 bytes

// For a total of 10 000 012 bytes


*** Test2 is 12 bytes in total : 4 + 2 + 2 + 1 + 1 = 10 rounded up to the
nearest multiple of int size, makes 12

So total size of Test2 array of 1000000 elements is 120012 bytes.

Note that this is valid for the current versions of the (32bit) CLR but is
in no way guaranteed, the CLR is free to map field elements as it sees fit.

Willy.


Aah.. Did some more tests with structs. The current CLR apparently
aligns structs to the biggest primitive in the struct.

So if you only have shorts and bytes, total struct memory size is
divideable by 2, but if you add a single long the total struct memory
size has to be dividable by 8.

Classes memory size however seem to only have to be dividable by 4
even though it contains long fields. Should have tested a little more
before posting I guess :)

As you said, and as I noted in my first post, this is all for the
current CLR and may differ between implementations .

--
Marcus Andrén
Nov 17 '05 #10

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

Similar topics

6
2741
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid = server.createobject("scriptlet.typelib").guid guid=Left(guid,instr(guid,"}")) genguid=guid
13
21357
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
19
2124
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit of a possibly large structure/field.
8
3503
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it gives error! In file included from main.c:2: a1.c:3: error: initializer element is not constant
4
1632
by: Murph | last post by:
Hi, I'm new to ASP.NET and have a problem which i'm hoping will be easy to answer... i have only used VB, HTML and Flash up until now! I am designing an ASP page which uses OleDb to retrieve data from an access database and show it on screen... however, i want the viewer to be able to select an item from a dropdown menu which relates to one of the fields and have only that information shown... how do i declare the variable?
7
2160
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've got is a decodeImage.aspx page that gets called to decode base64 encoded images and return them as displayable images to a Web page. The ASPX page is passed two parameters, "file" and "img". The "file" parameter specifies and XML file on the Web...
8
10154
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
6
4667
by: vaidehikedlaya | last post by:
Hello, I am using gmp.h library. I am trying to put mpz_t variable into char buffer and back. I came across mpz_import/mpz_export to suffice this purpose. But, I am not very clear about using these functions. I am not getting the right result for the program below. In the program I have included the code(present in the gmp document, in the description of mpz_export function) numb = 8*size - nail;
13
2008
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable "myvariable" and then maybe assign it something. myvariable = "this is a real variable"
7
2785
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length array type of unspeciï¬ed size, which can only be used in declarations with function prototype scope;124) such arrays are nonetheless complete types. If the size is an integer constant expression and the element
0
8004
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
7934
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
8425
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...
1
8071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8288
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
6743
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
5886
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
3912
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
1271
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.