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

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 3351
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

"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.

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*******@gmail.com> wrote in message
news:OC**************@TK2MSFTNGP09.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*******@gmail.com> wrote in message
news:OC**************@TK2MSFTNGP09.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*****@NOSPAMoptushome.com.au> wrote in message
news:uQ*************@tk2msftngp13.phx.gbl...
"jeremiah johnson" <na*******@gmail.com> wrote in message
news:OC**************@TK2MSFTNGP09.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**********@discussions.microsoft.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,char
4 int,uint,float
8 long,ulong,double

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,Reference: 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.com...
On Mon, 3 Oct 2005 14:24:02 -0700, TheMadHatter
<Th**********@discussions.microsoft.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,char
4 int,uint,float
8 long,ulong,double

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,Reference: 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
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:O2****************@TK2MSFTNGP09.phx.gbl...
This isn't correct also, the CLI Boolean type is 8 bits, the language has
nothing to do with this


Really? Doesn't vb6 use 16 bits? :-)

Michael
Nov 17 '05 #11

"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:O4*************@TK2MSFTNGP12.phx.gbl...
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:O2****************@TK2MSFTNGP09.phx.gbl...
This isn't correct also, the CLI Boolean type is 8 bits, the language has
nothing to do with this


Really? Doesn't vb6 use 16 bits? :-)

Michael


Since when does VB6 target the CLI and the CTS? Whe are talking about
managed languages here don't we?

Willy.


Nov 17 '05 #12
Willy,

The only most optimized value type is for me the size as is used by the
register. What is on 32Bits computers the 'int'. That there is than a loss
of memory with the for most programs probably not more than 10 booleans
which in memory are used (not all the ones in the programs, those will be
released) is for me complete not important.

I find it a pity that the 'int' will stay on a 64bit computer 32bits and
therefore need converting all the time. (And to be changed by hand in future
to get them optimized).

I find it strange that guys as Jon, who are forever for the most optimized
code, do not pay attention to this.

Just my idea

Cor
Nov 17 '05 #13
Cor,

inline ***

Willy.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
Willy,

The only most optimized value type is for me the size as is used by the
register. What is on 32Bits computers the 'int'. That there is than a loss
of memory with the for most programs probably not more than 10 booleans
which in memory are used (not all the ones in the programs, those will be
released) is for me complete not important.
***
I'm not clear on what you mean by this, you don't want a char type to take
32 bits (or worse 64 bits) in memory don't you?
I find it a pity that the 'int' will stay on a 64bit computer 32bits and
therefore need converting all the time. (And to be changed by hand in
future to get them optimized).
***
Most modern (are there other) 64 bit CPU's have a register sets that can
store (individual addressable) 8, 16 , 32 and 64 bit "entities", the kind of
register used depends on the effective operand size. All 64 bitters I know
of default to 32 bit operand sizes, other register sizes must be explicitely
indicated by means of an operand or instruction prefix.
That means that CLI int's can be stored as 32 bit entity in a "32 bit
register", while CLI long's will be stored in a 64 bit registers, no
conversion is needed here.

I find it strange that guys as Jon, who are forever for the most optimized
code, do not pay attention to this.

***
Don't know exactly what you mean by this, did I miss another discussion
about this all?

Nov 17 '05 #14
Willy,

I think that the importunacy in what way a value is stored in memory is from
40 years ago or can be important on a PDA (although that is probably gone as
well).

The way a value is processed is in my opinion much more important.

***
Don't know exactly what you mean by this, did I miss another discussion
about this all?

Probably in the VBNet newsgroup and Chats this is discussed. Keeping the int
(Integer) as 32Bit will have a slight negative influence on the performance
is told in those discussions.

Cor
Nov 17 '05 #15
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eX*************@TK2MSFTNGP10.phx.gbl...
Since when does VB6 target the CLI and the CTS? Whe are talking about
managed languages here don't we?


The OP is, but when I said it varies from language to language I was not.

Michael
Nov 17 '05 #16
I clearly said "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". And then
you start about VB6 while no-one else is talking about non CLI languages,
that way you are allways right. Be more specific next time or stay on topic.

Willy.
"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eX*************@TK2MSFTNGP10.phx.gbl...
Since when does VB6 target the CLI and the CTS? Whe are talking about
managed languages here don't we?


The OP is, but when I said it varies from language to language I was not.

Michael

Nov 17 '05 #17
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I clearly said "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". And
then you start about VB6 while no-one else is talking about non CLI
languages, that way you are allways right. Be more specific next time or
stay on topic.


Well sorry your magesty.

Michael
Nov 17 '05 #18

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

Similar topics

6
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 =...
13
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
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...
8
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...
4
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...
7
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...
8
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". */...
6
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...
13
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...
7
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
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,...
0
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...

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.