Well, the reallocate memory is having no effect and, as far as I know, you shouldn't be doing that.
I think the heart of your problem is in knowing when to allocate memory (and thus use a pointer) and when you cannot and must not do this. Of course this all arises from your wish to use something like childs.child[0] in which you are 'tricking' Purebasic into allowing a dynamic array within a structure (arrays in structures are inherently static).
Let me see if I can explain?
If you are creating a static structure as in :
Code: Select all
Structure main
something.l
childs.child[10]
EndStructure
world.main
then with the statement 'world.main' Purebasic has already allocated memory for the variable world (44 bytes in this case). Hence you must not attempt to allocate memory and set world equal to the resulting buffer. That is do not attempt writing 'world = AllocateMemory(...)' etc.
On the other hand, a dynamic structure such as in :
Code: Select all
Structure main
something.l
childs.child[10]
EndStructure
*world.main
then with the statement '*world.main' we are telling PB that *world will point to a chunk of memory representing a structure of type main. However, this pointer has not been initialised and points to nothing. This is where you might allocate some memory with :
Code: Select all
*world = AllocateMemory(SizeOf(main))
Now *world is initialised we can use it as if it was a regular structure.
The complication is when you switch childs.child[10] for childs.child[0], but I'll return to that later.
In terms of getting your code working, you need a pointer to the main structure :
Code: Select all
Structure child
something.l
; simplified for test
EndStructure
Structure main
something.l
childs.child[0]
EndStructure
elements = 100
*world.main = AllocateMemory(SizeOf(child)*elements+SizeOf(LONG)) ;Extra 4 bytes for 'mains' something field.
*world\childs[85]\something = 123456
Debug PeekL(*world\childs[85])
Now, in using childs.child[0] we are stepping up a degree of complexity here. Because you are tricking PB into allowing a dynamic array in a structure (which isn't catered for because of compatability issues with the win api!) you cannot expect PB to set aside enough memory for an array whose upper bound is subject to change etc. Hence the need to dynamically allocate memory and this forces us to use at least one pointer (as above).
Compare this use of a single pointer, however, with the hundreds you had in your earlier code above!
I hope this helps.