Implementing handles

Just starting out? Need help? Post your questions and find answers here.
blueapples
User
User
Posts: 19
Joined: Mon Oct 03, 2005 10:00 am

Implementing handles

Post 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?
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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
I may look like a mule, but I'm not a complete ass.
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

ReDim should be no problem with structured arrays.
blueapples
User
User
Posts: 19
Joined: Mon Oct 03, 2005 10:00 am

Post by blueapples »

Weird... I could have sworn I read in the help file that ReDim clear memory of the array. Never mind then, thanks!
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Post Reply