SA-MP Forums Archive
dynamic array allocation - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: dynamic array allocation (/showthread.php?tid=435837)



dynamic array allocation - Dj_maryo1993 - 08.05.2013

Ok so i got an unkdown number of vehicles , wich i load from mysql
Curently i have the variable defined as this
pawn Код:
new CarInfo[MAX_VEHICLES][cInfo];
I reaaly thot about a way of replacing MAX_VEHICLES with the exact number of the vehicles from mysql , but the question is , can i change the array size of a variabile after being created ? If not , can any1 thinks of a way to do this ?

PS :Ive just given a example , i also load things like Faction/gates/pickups and much more from mysql


Re: dynamic array allocation - CJay9209 - 08.05.2013

I think you would have to undefine MAX_VEHICLES the just define it again. I'm not sure that's such a good idea though as that may make your array go out of bounds.

In fact I'm not even sure the code would compile TBH. The array size I THINK has to stay static which means setting it to the MAX number of vehicles possible on the system.


Re: dynamic array allocation - IceCube! - 08.05.2013

#if defined MAX_VEHICLES
#undef MAX_VEHICLES
#define MAX_VEHICLES number
#endif


Re: dynamic array allocation - Bakr - 08.05.2013

Not natively, in Pawn array sizes need to be known at compile time.

You can download one of the two plugins that give dynamic memory allocation capabilities: Vectoral Pawn & CSTL.


Re: dynamic array allocation - CJay9209 - 08.05.2013

lol I'm waiting for the array out of bounds errors...

Changing an array size mid script is a horrible idea unless your entire script is retrofitted to handle it.

For Example.

- loop starts using MAX_VEHICLES as an end point
- Array size changes mid loop
- CRASH!!!!!!!! (or maybe not but it'll feck with ur code)