Calculating Array Memory usage. How?

Blitz3D Forums/Blitz3D Beginners Area/Calculating Array Memory usage. How?

Galdy(Posted 2011) [#1]
Can some one tell me how to calculate how much memory an array would use?


Matty(Posted 2011) [#2]
If it is an array of floats or integers then the number of elements in the array multiplied by 4 is the amount of memory (in bytes) used by the array.

Eg:
Dim MyArray(10000) is an array of integers with 10001 (including '0' as the first element) elements which is 40004 bytes, or roughly 40kb...pretty small.

The same would be the case if it was an array of floats, however an array of strings I'm not sure how blitz handles the memory for one of these, I would imagine it would be 4 bytes per element where the string is empty (header of 4 bytes indicating length of string), and 4 bytes per element plus length of the string, where the string is not empty, in bytes.

To give you an example though, if you wanted to take up 100MB of RAM then with an array of integers or floats you'd need roughly
25,000,000 elements in your array.....
ie Dim MyArray(25,000,000) would roughly equate to a 100MB array.

Last edited 2011

Last edited 2011


Galdy(Posted 2011) [#3]
Ok. Then I have plenty of memory for my application since it just involves intergers. Thanks.


_PJ_(Posted 2011) [#4]

Dim MyArray(10000) is an array of integers with 10001 (including '0' as the first element) elements which is 40004 bytes, or roughly 40kb...pretty small.


The Array will contain EXACTLY 40004 Bytes which is EXACTLY 39kb and 68bytes.

For 100Mb, you would need 26214400 4-byte elements in an array.

floats and Ints 4 Bytes
Shorts = 2 Bytes (Obviously)*
Strings = length+4 Bytes
Bytes = 1 Byte (Obviously)*

* However, these are only ever relevant, when used with Poke or Write, otherwise, all instances will be 4-Bytes.

When arrays are of strings, the length-byte is still stored**


For Example
Local bBank=CreateBank(4)
For f=0 to 3
PokeByte(bBank,f,Rand(256) Mod 256)
Next

MyInt=PeekInt(bBank,0)
MyByte=PeekByte(bBank,0)
FreeBank bBank

Despite the fact only 1 byte is read with PeekByte, MyByte will actually contain 4 bytes. Three of which are efrfectively wasted.

I'm not sure if it's possible to make a custom typedef of a variable type to only accept 1 byte, you might try:

bbyte=(Rand(256) Mod 256)+(Rand(256) Mod 256)Shl 8+(Rand(256) Mod 256)Shl 16+(Rand(256) Mod 256)Shl 24

.Byte bbyte=(bbyte And 255)
Print .Byte bbyte


But still, there's no guarantee bbyte isn't just going to default to a 4-Byte integer again :S


Yasha(Posted 2011) [#5]
I'm not sure if it's possible to make a custom typedef of a variable type to only accept 1 byte


....wut?

Blitz3D doesn't support the typedef command... and judging by the syntax of the code example... have you been smoking C++?


Warner(Posted 2011) [#6]
Strangely enough, that code seems to run without an error. But it doesn't create an 8-bit type tho. It just seems to ignore the ".Byte" part right after Print. At least, when I tried using "goto Byte", it jumped to the first .Byte label, and when I removed that, it complained about an undefined label.
Maybe the second ".Byte" is removed during one of the (pre)parsing steps in compilation?


_PJ_(Posted 2011) [#7]


Strangely enough, that code seems to run without an error.


Why wouldn't it?

In Blitz "." either refers to a custom object or a label.

If you are using Goto, it's treated as a label.


Yasha(Posted 2011) [#8]
In Blitz "." either refers to a custom object or a label


It is the type signature for variables of user-defined type, in which case it follows the variable name. Type never precedes variable-name in B3D - that's C syntax! And it can't refer to a type if the type hasn't been defined.

Aside from that, what has the first line of the example - combining randomised bytes into one integer - got to do with typedef, which doesn't exist in any Blitz language (again, C)?

Last edited 2011


Warner(Posted 2011) [#9]
Why wouldn't it?

1. There is a label in the middle of a Print statement or a type identifier that defines "Print" as a Byte, followed by an undefined function without brackets.
2. Depending on the above, there is either a duplicate label or no Type Byte defined.

A 32-bit processor deals with 4 bytes at a time. So unless you're packing bytes into groups of four and deal with them at the same go, a Byte would use up as much processing power as an Int.
I think that is the reason why there is only Peek/PokeByte and Read/WriteByte and no Byte prototype.


_PJ_(Posted 2011) [#10]
hrmm... sorry about that. Looking back on it, I dunno what I was thinking >.<

Very bad, bad syntax and imporper use indeed.

Seems Blitz just interpreted the first .Byte as a label, since there was no stopping the runtime execution, it continued past it ignoring it completely. (Which is normal)

When it came to the second one, though, it also seemed to ignore it, even if it thought it was a label it shoulda picked up on the duplication. You're right, it's wo weird that didn't throw an error, either invalid type object or the duplicate label!