Dim Quuestion...

Miscellaneous Forums/General Discussion/Dim Quuestion...

if i do something like this...
Dim X(500)
1- have i just reserved 500 memory locations * size of INTEGER ?

2- if i don't use all of them, the memory is still reserved and thus wasted, right?

3- is there any way to redim and preserve the array contents?

4- if i try to access X(200) without first assigning it a value, i'll get what returned... zero or null, or will it raise an exception... i mean error?

thx again...

--Mike

Posting from knowledge off the top of my head, may or may not be true.

1- have i just reserved 500 memory locations * size of INTEGER ?

Yes.

2- if i don't use all of them, the memory is still reserved and thus wasted, right?

Yes.

3- is there any way to redim and preserve the array contents?

Would need to write your own routine, I know quite a few people stored the array in a bank, resized it, then transferred it back to the newly dimmed array.

4- if i try to access X(200) without first assigning it a value, i'll get what returned... zero or null, or will it raise an exception... i mean error?

This I cant remember too clearly, but I'd assume it would return a 0 in B3d because of the way it works.

1 yea
2 guess so :P
3 use a temp array to save the contents?
4 0

I assume you're not talking BMax right now?

Why not try banks...
- you can also store bytes and shorts then
- you can resize without loosing contents
- can be created/free'd locally

1- have i just reserved 500 memory locations * size of INTEGER ?
Yes

2- if i don't use all of them, the memory is still reserved and thus wasted, right?
Yes

3- is there any way to redim and preserve the array contents?
Slices if it's Bmax but then it woudn't be a 'dim' command.

4- if i try to access X(200) without first assigning it a value, i'll get what returned... zero or null, or will it raise an exception... i mean error?
0

thx guys... not enough caffeine this morning... the ole brain matter is still in stasis...

thx again...

--Mike

at least the array is zerod, some languages it isn't, it's just whatever was in the memory before!

I hate not being able to create a dynamically sized array in BPlus.

1 - 501 * size of INTEGER (0 -> 500)

Cor, blimey. It took enough posts before Alfred gave the correct answer to question 1! And you call yourselves programmers!? :P

And you call yourselves programmers!? :P


Been programming too much Java lately, which does the correct way of dimming arrays to my knowledge. >.>

OK Genexi2, I'll let you off this time. :P

yeah... i had to take a second look when i read that Dim Days(6) created a 7 creates a 7 element array...

it's convenient, but definitely doesn't go along with convention...

by the way... is there a way to query the maximum array size available for a particular array?

--Mike

Array size is only limited by available mem, AFAIK.

yeah big10... but what i need to know is, if i DIM days(6), is there any way to retrieve the number of elements i alloted for it...

like this maxDays=top(days)

top returns the DIMmed number

--Mike

There's no built-in way of doing that. You'd just have to keep track of the size manually, everytime you reDIM the array.

Banks, on the other hand, have the BankSize command, and (as has already been said) let you resize without losing contents. The cost: banks are slower than arrays.

Could you not just do a loop through the array until you hit a null pointer to find out the arrays size, or is there not a way to trap the error and let the program continue as normal?
(havent touched Blitz in over a year here >.> )

In blitz, Null is 'special' and can only be used with type instances.

yeah... i'm using a global now... i was lookning for a way to make the OO subClass creation in my lil OO game engine thingee a lil less clumsy looking, and have it taken care of without having to code it...

aaahhhh, it's ok...

thx again...

--Mike

Yeah, that's the best you can do, Red. Blitz3D only does array bounds checking when in debug mode, for speed reasons. Personally, I'll take the speed in place of the idiot-proofing, every time! :P

I think the max size of an array, regardless of dimensions, is 2 gigabytes due to signed 32 Bit adressing.
To determine the size of an array you can store the value you used to dim it somewhere, eg in the first index or if the array is part of a type structure you may store the array size in a type variable. You may also completely replace blitzarrays by banks, that's pretty simple. The pro of banks is they can be created and deleted really dynamicly. Where you need to dim an Array at least one time in the main program explicitly to be able to use or redim it in any function, banks can be created spontanously. It's very handy when you can create a bank in a function, poke all kinds of info to it and then return its handle.

Word to the perhaps-wise: use banks.

I think the max size of an array, regardless of dimensions, is 2 gigabytes due to signed 32 Bit adressing.
Hmm, not sure about that, since a multi-dimesional array is basically an array-of-arrays. Thus, each 'sub' array is only limited by the signed 32bit address space.

I think that isn't correct. The adress is calculated by index1*index2*index3...*4. I guess unsigned 32 Bit is used internally as well. At least on a win98 system you cannot use 64 Bit adresses anyway, AFAIK.

EDIT: wait a minute, this is strange:

I can DIM a(0,$7fffffff), although this are 8 billion bytes, but in fact I only got 700 Megs of ram. Nevertheless when I assign a value of 123 to the index 0,$7fffffff, it's stored correctly.

But this is weird: when I DIM a(0,100000000) (hundred megs) I get a MAV?!?

Well not sure about this whole thing, I think i got to have a closer look.

Red: If it was true OO, you could make your own array class with functions to return top, resize, sort etc. Of a decent OO language already has an array class, BlitzMax does too, I believe.

yeah... that would be the ideal situation... then i wouldn't have to hack all this stuff out... an i wouldn't need arrays to associate am instance property with a class method...

i may wind up getting a BMax license and looking at Thierry's 3D engine...

--Mike

i wouldn't need arrays to associate am instance property with a class method


If you want to take the OO approach, look into using my OO Container Classes for Blitz3D.

P.S. I would write DIM days(7-1). Do you people really need a function to subtract one from an integer?

Cor, blimey. It took enough posts before Alfred gave the correct answer to question 1! And you call yourselves programmers!? :P

I know that, but I don't waste time thinking about it. I never use the 0 slot because that's not how I count. I suppose that's one integer of wasted RAM; oh noes!

hey octo... that looks really decent... thx

--Mike

I always use the 0 slot. I define all arrays by first making a const say MAX_BULLETS and the doing Dim Bullets.Bullet(MAX_BULLETS-1). Then all loops can either be 0 to MAX_BULLETS-1 or For b.bullet = Each Bullet. You could argue that using 1 to MAX_BULLETS is faster as no -1 is needed but I'd hope the -1 would be applied at compile time, plus I'm old school anyway.

Some languages give nice errors when you access out of bounds arrays but C used to just write to the memory and/or crash so I learnt to be careful :-)