Return pointer to new variable in procedure

Just starting out? Need help? Post your questions and find answers here.
wb2k
User
User
Posts: 18
Joined: Sun Feb 09, 2014 1:22 am

Return pointer to new variable in procedure

Post by wb2k »

I want to create a pointer to a new variable in a procedure. When declaring the variable with Static the content of the variable is overwritten on the second run which seems to what the documentation of Static says that the variable isn't reinitialized:

Code: Select all

Structure sum
  sum.i
EndStructure

Procedure add(a,b)
  Static foo.sum
  foo\sum = a+b
  ProcedureReturn @foo
EndProcedure

*aaa.sum = add(1,2)
Debug *aaa\sum

*bbb.sum = add(3,4)
Debug *bbb\sum
Debug *aaa\sum
aaa.sum is correctly set to 3, then bbb.sum is correctly set to 7, but also aaa.sum is (wrongly) set to 7.

However, when using Protected where the variable would be reinitialized every time the debug output shows which seems to be a pointer and also 3 times the same pointer-address.

Does anyone have an idea how I can achieve this?
infratec
Always Here
Always Here
Posts: 7604
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Return pointer to new variable in procedure

Post by infratec »

If you want a new 'variable' you have to create it.
And... free it.

Code: Select all

Procedure add(a,b)
  Protected *a.integer
  
  *a = AllocateMemory(SizeOf(Integer))
  
  *a\i = a+b
  ProcedureReturn *a
EndProcedure

Define *a.Integer, *b.Integer

*a = add(1,2)
Debug *a\i

*b = add(3,4)
Debug *b\i
Debug *a\i

FreeMemory(*a)
FreeMemory(*b)
wb2k
User
User
Posts: 18
Joined: Sun Feb 09, 2014 1:22 am

Re: Return pointer to new variable in procedure

Post by wb2k »

Thank you very much infratec! :D

So here is the working example with the structure (without freeing memory):

Code: Select all

Structure sum
  sum.i
EndStructure

Procedure add(a,b)
  Protected *foo.sum
  *foo = AllocateMemory(SizeOf(sum))
  *foo\sum = a+b
  ProcedureReturn *foo
EndProcedure

*aaa.sum = add(1,2)
Debug *aaa\sum

*bbb.sum = add(3,4)
Debug *bbb\sum
Debug *aaa\sum
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: Return pointer to new variable in procedure

Post by mestnyi »

infratec wrote:If you want a new 'variable' you have to create it.
And... free it.

Code: Select all

Procedure add(a,b)
  Protected *a.integer
  
  *a = AllocateMemory(SizeOf(Integer))
  
  *a\i = a+b
  ProcedureReturn *a
EndProcedure

Define *a.Integer, *b.Integer

*a = add(1,2)
Debug *a\i

*b = add(3,4)
Debug *b\i
Debug *a\i

FreeMemory(*a)
FreeMemory(*b)
I do not see to be freed.

Code: Select all

Procedure add(a,b)
  Protected *a.integer
 
  *a = AllocateMemory(SizeOf(Integer))
 
  *a\i = a+b
  ProcedureReturn *a
EndProcedure

Define *a.Integer, *b.Integer

*a = add(1,2)
Debug *a\i

*b = add(3,4)
Debug *b\i
Debug *a\i

Debug "free"
FreeMemory(*a)
FreeMemory(*b)

Debug *b\i
Debug *a\i
User avatar
mk-soft
Always Here
Always Here
Posts: 6242
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Return pointer to new variable in procedure

Post by mk-soft »

The last access takes place on not yet overwritten memory. May lead to memory access errors.

It is easier to transfer the result via parameter ByRef

Code: Select all

Procedure add(a, b, *result.integer)
  *result\i = a + b
EndProcedure

Define a.i = 10
Define b.i = 20
Define c.i

add(a, b, @c)
Debug c
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply