Share your advanced PureBasic knowledge/code with the community.
Psychophanta
Always Here
Posts: 5153 Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:
Post
by Psychophanta » Fri Oct 07, 2005 10:50 pm
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
Psychophanta
Always Here
Posts: 5153 Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:
Post
by Psychophanta » Wed Apr 25, 2007 5:24 pm
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
Fred
Administrator
Posts: 18162 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Wed Apr 25, 2007 6:19 pm
You're really writing at forbidden place with your second example..
Kaeru Gaman
Addict
Posts: 4826 Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany
Post
by Kaeru Gaman » Wed Apr 25, 2007 6:21 pm
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.
netmaestro
PureBasic Bullfrog
Posts: 8451 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Wed Apr 25, 2007 6:33 pm
What's "shoting your billies"
BERESHEIT
Kaeru Gaman
Addict
Posts: 4826 Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany
Post
by Kaeru Gaman » Wed Apr 25, 2007 6:42 pm
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.
Psychophanta
Always Here
Posts: 5153 Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:
Post
by Psychophanta » Wed Apr 25, 2007 6:42 pm
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
tinman
PureBasic Expert
Posts: 1102 Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:
Post
by tinman » Wed Apr 25, 2007 8:51 pm
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)
Psychophanta
Always Here
Posts: 5153 Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:
Post
by Psychophanta » Wed Apr 25, 2007 8:57 pm
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