Getting length of array?

Started by SolarLune, December 04, 2010, 07:47:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SolarLune

Hey. So I was wondering if there's a way to get the length of an array - I would rather not count the number of objects (integers) in the array manually. I tried both len(array) and sizeof(array), but neither worked (by the way, sizeof isn't in the BennuGD Wiki). Does anyone know of a way to figure this out? What's more, you can access values of an array that don't have any assigned variable (i.e. you make an array with size 20, but accessing array[50] returns 0). That's a bit... Odd. Does anyone know anything about this?

osk

Sizeof() returns the total amount of bytes occupied by a variable or array.
So, if you do numbers_elements=sizeof(array_name)/sizeof(array_name[0]) , you will get what you want.

handsource-dyko

Acessing things beyond the array bounds?
As far as I know, bennu won't allow you to do this.
Bgdi should complain about this, because bennu has some run-time
checking built-in.

Drumpi

#3
If you want to know the lengh of an array, check it's own declaration, you only can use a constant to define it and cannot resize.
The only way to resize an array is using pointers.

If you try to access to a position out of the array, if you use a constant, Bennu will get you an error in compilation time. If not, if you use a variable, maybe you don't get an error (some OS give you a several error, some others not, and sometimes SDL give you a segmentation fault), but you are accesing to a random part of memory and can crash all the system with a critical error, it's so serious, so be carefull when you are doing that.
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)

SolarLune

Yeah, I forgot that I made my array size 100, so even numbers that I didn't assign were 0. As for just checking the constant, that's slower than getting the actual size of the array (what happens if I need to check the size of a pointer to another array?), and I would rather not count the number of values I have in each of my arrays (I have an animation system that I'm using).

Drumpi

Maybe you can use the first position of the array to add the number of positions of this array, or last position avaiable.
But i don't think to read a constant value is slower than call a function who tells the array lenght. by the way, i don't think there is much difference using one or other.

Anyway you must use the functions you want, i'm only doing some suggestions ;)
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)

SolarLune

Hey, thanks for the help - I stalled on trying to figure out a way to pass an array to a function to figure out the length; couldn't figure out how to pass it using a pointer... Anyone know anything about this?

handsource-dyko

Pointers are a scary concept that I still don't fully grasp. But maybe you can do it the easy way: each time you chance something to
your arrray, increase or decrease a counter. I always put something like a used=True or used=False property in may array, so I can check in a For loop if a slot in the array is occupied. I know it requires to iterate through the entire array, this gets slow on large arrays, but, so this time is constant, since the size of the array is fixed at compile time.

I've always wanted to experiment with linked lists though, but again that means using pointers.
Maybe someone could do some linked list example that is specific for bennu, because translating an example from C to bennu is a bit
awkward to me.

Drumpi

I wrote a little tutorial about dinamic arrays using pointers, but is in spanish, in "ayuda" subforum.
http://forum.bennugd.org/index.php?topic=988.0

Maybe a google translate could help a little, i could do a worst translation ^^U
Hala, como con 1001 procesos sólo va a 9 FPS, vamos a meterle 32 veces más, a ver si revienta.
(Drumpi epic moment)