Easy way to perform a dynamic array

Share your advanced PureBasic knowledge/code with the community.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Easy way to perform a dynamic array

Post by Psychophanta »

This is referred to viewtopic.php?p=105878
This example shows how can be done a dynamically resizeable variable type and its dimensioned array(s) inside.
Probably it is not very fast if top speed required, but it is good for normal apps.

Code: Select all

Structure su
  i.l[0]
EndStructure

*kz.su = AllocateMemory(SizeOf(su))

For t=0 To 300
  *kz.su = ReAllocateMemory(*kz.su,(t+1)*SizeOf(su))
  *kz\i[t]=5+t
  Debug *kz\i[t]
Next
Edit: Updated to PB4.0
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Previous code does work for PB4.00, but it does not for PB4.02.
Here is the update for 4.02:

Code: Select all

Structure su 
  i.l[0] 
EndStructure 

kz.su

For t=0 To 300
  kz\i[t]=5+t
  Debug kz\i[t]
Next
nice! doesn't :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You're really writing at forbidden place with your second example..
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

that looks really, really odd for my eyes.

declaring a single variable but using it as a neverending array?
where is memory protection?

when this works, then just because you do not notice how you're shoting your billies....

it's like the famous word:
"I allocate 4 Byte, but write 5 Byte into it. On modern PCs is always enough capacity avaliable, so it will work"

PS:
ok, Fred said it more clearly and less comedian like...
oh... and have a nice day.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

What's "shoting your billies" :?:
BERESHEIT
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

that was impro...

meaning "overwriting other memory you have 'billed' (allocated) before"

...was an improvisation using my second language in an "artist" way...
oh... and have a nice day.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Fred wrote:You're really writing at forbidden place with your second example..
Pity that the link i refer in 1st post doesn't work. There you said (when PB3.94) that it was safe.
Well, then

Code: Select all

Structure su
  i.l[0]
EndStructure

*kz.su = AllocateMemory(SizeOf(long))

For t=0 To 300
  *kz.su = ReAllocateMemory(*kz.su,(t+1)*SizeOf(long))
  *kz\i[t]=5+t
Next
For t=0 To 300
  Debug *kz\i[t]
Next
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Psychophanta wrote:
Fred wrote:You're really writing at forbidden place with your second example..
Pity that the link i refer in 1st post doesn't work. There you said (when PB3.94) that it was safe.
Perhaps it was safe that zero sized arrays in structs would be safe to use for this purpose in the future (i.e PB4 at the time)?

The link looks like it has an extra digit, I tried various combinations but couldn't find the topic.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Hi tinman, you have been offline a lot of months :!:
I believed you dissapeared like Danilo and others :?

Nah! the link is well written, but i dunno why it doesn't work. It must be lost.

EDIT (2007-10-12): Useless trick, since the invention of ReDim() command:

Code: Select all

Dim kz.l(0); <- use any varible type and/or more than one dimension ;)
For t=0 To 300
  ReDim kz(t)
  kz(t)=5+t
Next
For t=0 To 300
  Debug kz(t)
Next
8)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply