Page 1 of 1
is this code legal???
Posted: Tue May 22, 2007 4:16 pm
by jacfrosty
Hi, could someone please tell me if this code is legal, if so then whats going on with it and if not - why not.
I was messing with pointers to structures and some how seem to have myself lost in the woods - so to speak.
Any info would be really appreciated.
Code: Select all
Structure testyItem
name.s{30}
x.l
y.l
len.l
wid.l
type.b
EndStructure
Structure testyStruct
name.s{30}
location.s{70}
*item.testyItem[0]
EndStructure
t.testyStruct
For a = 0 To 999
t\item[a] = AllocateMemory(SizeOf(testyItem))
Next
t\name="parts"
t\location="c:\DatabaseDir"
t\item[0]\name = "partno"
t\item[1]\name = "partdesc"
Debug SizeOf(t)
Debug SizeOf(t\name)
Debug SizeOf(t\item)
The results of the debugs are interesting as the last one is zero..
Kind regards
David
Posted: Tue May 22, 2007 4:50 pm
by Pupil
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

Posted: Tue May 22, 2007 4:55 pm
by jacfrosty
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..

Posted: Tue May 22, 2007 5:05 pm
by Pupil
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.
Posted: Wed May 23, 2007 9:45 am
by jacfrosty
Thanks for that Pupil,
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..
So I was thinking something along these lines
Code: Select all
Structure testyItem
name.s{30}
x.l
y.l
len.l
wid.l
type.b
EndStructure
Structure testyStruct
name.s{30}
location.s{70}
*item.testyItem[0]
EndStructure
t.testyStruct
t\item = AllocateMemory(4*1000)
For a = 0 To 999
t\item[a] = AllocateMemory(SizeOf(testyItem))
Next
t\name="parts"
t\location="c:\DatabaseDir"
t\item[0]\name = "partno"
t\item[1]\name = "partdesc"
Debug SizeOf(t)
Debug SizeOf(t\name)
Debug SizeOf(t\item)
Would that be OK??
Kind regards
David.
Posted: Wed May 23, 2007 6:25 pm
by Pupil
No, i think that code would have the same problem as the previous one.
I've made two small examples that you should be able to use to solve your problem:
Code: Select all
; 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
Posted: Thu May 24, 2007 10:00 pm
by jacfrosty
Thanks very much for your help Pupil, greatly appreciated..
I think I understand where I was going wrong.
I am from a C background and things are done a little different when it comes to pointers and PureBasic.
Thanks for the examples.