In a bigger program you'll probably owerwite quite a lot of other variables using that code. The problem is the '*item.testyitem[0]' member of the structure 'testyStruct'. You're not allocating any space for the pointers so when you use 't\item[a]' you're actually accessing the memory '@t+sizeof(testyStruct)+4*a', which is bad
I did think that that should be the case, but as it compiles and runs without errors I thought that something magic may have been going on, i.e. PureBasic may be allocating memory for the pointers or something.
I still can't get my head around why this works without instantly blowing up my computer as it would appear to be so very very dangerous..
If you increase maxvalue in the FOR-loop you'll probably have a crash. I think windows allocates a whole page or something and wont complain until you try to poke outside this memory chunk.
Ok then, is there a way that I can allocate memory for the array of pointers before I allocate the space that each pointer points to??
Basically I want to have a structure that can point to an array of structures that are of different sizes (when I say sizes I mean the no of elements in the array, not a different physical array size) - I hope I have explained what I mean correctly there..
; Example of one way to solve problem:
Structure testyItem
name.s{30}
x.l
y.l
len.l
wid.l
type.b
EndStructure
Structure ItemArray
*item.testyItem[0]
EndStructure
Structure testyStruct
name.s{30}
location.s{70}
*array.ItemArray
EndStructure
t.testyStruct
t\array = AllocateMemory(4*1000)
For a = 0 To 999
t\array\item[a] = AllocateMemory(SizeOf(testyItem))
Next
;----------------------
; Another way to do it:
Structure testyStruct1
name.s{30}
location.s{70}
*item.testyItem[0]
EndStructure
arraysize.l = 1000
*tmp.testyStruct1 = AllocateMemory(SizeOf(testyStruct1)+ 4*arraysize)
For a = 0 To 999
*tmp\item[a] = AllocateMemory(SizeOf(testyItem))
Next