variable size?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
TheMadHatter said:
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.
 
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
 
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 said:
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
 
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
 
Michael C said:
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.
 
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 = 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
 
Some small corrections (***).

Willy.

Marcus Andrén said:
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 = 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.
 
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.
 
Willy Denoyette said:
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
 
Michael C said:
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.
 
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
 
Cor,

inline ***

Willy.

Cor Ligthert said:
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?
 
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
 
Willy Denoyette said:
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
 
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.
 
Willy Denoyette said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top