Page 1 of 1

Implementing handles

Posted: Thu Nov 13, 2008 5:53 am
by blueapples
How are handles implemented internally in PB? I would like to provide an "object handle" type interface in a dynamic library I am creating, but I noticed that if I use redim to resize an array of structures, the contents are trashed. Is there a way to do this sort of thing and not have to copy all the data to a new array?

Maybe dynamically allocate memory blocks and treat it as an array somehow?

Posted: Thu Nov 13, 2008 7:14 am
by pdwyer
From the manual
ReDim is used to 'resize' an already declared array while preserving its content. The new size can be larger or smaller, but the number of dimension of the array can not be changed.
You can just allocate a memory block using allocatememory and use pointer arithmatic on it.

*ByteArray\b = 127

etc

Posted: Thu Nov 13, 2008 10:37 am
by srod
Yes I ReDim structured arrays all the time and the contents have never been trashed yet!

E.g.

Code: Select all

Dim a.point(10)
a(1)\x = 20

Debug a(1)\x

ReDim a.point(100)
Debug a(1)\x

Posted: Thu Nov 13, 2008 12:36 pm
by Fred
ReDim should be no problem with structured arrays.

Posted: Thu Nov 13, 2008 5:15 pm
by blueapples
Weird... I could have sworn I read in the help file that ReDim clear memory of the array. Never mind then, thanks!

Posted: Fri Nov 14, 2008 1:47 am
by pdwyer
Thinking of a different language?

Some basics have redim clear the array unless the "Preserve" keyword is used. PB clears the array if Dim is used on it again.

amounts to the same thing I guess