Wrong ellement in array pointer

Just starting out? Need help? Post your questions and find answers here.
RE-A
User
User
Posts: 39
Joined: Thu Aug 21, 2008 4:19 pm
Location: Belgium
Contact:

Wrong ellement in array pointer

Post by RE-A »

I have a strange problem with an array pointer returned from a procedure. I think this is a bug but I'm not sure.

The b(0)\a element is not correct returned from the procedure until you uncomment the "Debug..." line :shock:

PB version 4.40 and Win Vista 32bit.

Code: Select all

Structure struc
  a.s
  b.l
EndStructure

Procedure.i proc()
Dim b.struc(2)

  b(0)\a = "ZZZZZZZZ" ;not the correct value
  b(0)\b = 4
  b(1)\a = "AAAAAAAA"
  b(1)\b = 2
  b(2)\a = "BBBBBBBB"
  b(2)\b = 3

  ;Debug "OK -> " + b(0)\a ;Uncomment this line and the element is correct returned.

  ProcedureReturn @b.struc()
EndProcedure

Dim test.struc(2)
*ptr.i = proc()
test() = *ptr

For iCount = 0 To 2
  Debug test(iCount)\a + " -> " + Str(test(iCount)\b)
Next
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Wrong ellement in array pointer

Post by srod »

Well, the array you declare within the procedure is local to the procedure; meaning that it's memory is released imediately prior to the procedure returning. Your code is thus flawed because you are returning a pointer to an array which has been released.

Declare the array as static and it should run fine.
I may look like a mule, but I'm not a complete ass.
RE-A
User
User
Posts: 39
Joined: Thu Aug 21, 2008 4:19 pm
Location: Belgium
Contact:

Re: Wrong ellement in array pointer

Post by RE-A »

thx, srod. That did it.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Wrong ellement in array pointer

Post by srod »

To be honest, it might be better simply creating your array in your main code and then passing the array as a parameter to the function etc.
I may look like a mule, but I'm not a complete ass.
RE-A
User
User
Posts: 39
Joined: Thu Aug 21, 2008 4:19 pm
Location: Belgium
Contact:

Re: Wrong ellement in array pointer

Post by RE-A »

I could narrow my problem down to the little code I posted but this function is part of a dll I'm creating which main purpose is to collect and transfer large array's of data structures from a datasection.
Post Reply