Page 1 of 1
structures as parameters (ByValue)
Posted: Sat Aug 14, 2010 7:59 pm
by IceSoft
I belive this is very important for some purebasic wrappers
Code: Select all
Structure myStruct
a.f
b.f
EndStructure
Procedure test(d.myStruct)
Debug d\a
Debug d\b
EndProcedure
aa.myStruct
aa\a = 10.0
aa\b = 5.0
test(aa)
Same for some return valueas as structures
Re: structures as parameters
Posted: Sat Aug 14, 2010 8:08 pm
by TomS
Code: Select all
Structure myStruct
a.f
b.f
EndStructure
Procedure test(*d.myStruct)
Debug *d\a
Debug *d\b
EndProcedure
aa.myStruct
aa\a = 10.0
aa\b = 5.0
test(aa)
Re: structures as parameters
Posted: Sat Aug 14, 2010 8:15 pm
by STARGÅTE
and for Return:
Code: Select all
Structure myStruct
a.f
b.f
EndStructure
Procedure test(*d.myStruct)
*d\a = 10.0
*d\b = 5.0
EndProcedure
test(aa.myStruct)
Debug aa\a
Debug aa\b
but you will:?
Code: Select all
Structure myStruct
a.f
b.f
EndStructure
Procedure.myStruct test()
d.myStruct
d\a = 10.0
d\b = 5.0
ProcedureReturn d
EndProcedure
aa.myStruct = test()
Debug aa\a
Debug aa\b
Right ?
but i thinks ist the same way

Re: structures as parameters
Posted: Sat Aug 14, 2010 8:32 pm
by KJ67
It is a nice suggestion I like it

, it would open up to clearer implementations of this kind of things.
Code: Select all
Structure Polynom
a.i: b.i : c.i
EndStructure
Structure PolynomProduct
a1.i: b1.i: c1.i
a2.i: b2.i: c2.i
a3.i: b3.i: c3.i
EndStructure
Procedure.PolynomProduct PMul(a.Polynom, b.Polynom)
Protected result.PolynomProduct
;... code...
ProcedureReturn result
EndProcedure
But there may be a huge chunk of data on the stack, just imagine...
Code: Select all
Procedure.iso_image CatchCDImage(Drive)
EndProcedure
Re: structures as parameters by value (not reference)
Posted: Sat Aug 14, 2010 8:53 pm
by IceSoft
By reference:
Procedure test(*d.myStruct)
by value:
Procedure test(d.myStruct)
Re: structures as parameters
Posted: Sat Aug 14, 2010 8:56 pm
by STARGÅTE
ah, you will work with "a copy" of structure in your procedure ...
Re: structures as parameters
Posted: Sat Aug 14, 2010 8:57 pm
by TomS
Code: Select all
By Value: aa\a = 10.0
By Value: test(aa)
Re: structures as parameters by value (not reference)
Posted: Sat Aug 14, 2010 9:21 pm
by Demivec
Code: Select all
;by value
Procedure test(*d)
Protected e.myStruct
CopyStructure(*d, e, myStruct)
Debug e\a
EndProcedure
test(aa.myStruct)
Re: structures as parameters by value (not reference)
Posted: Sat Aug 14, 2010 9:59 pm
by IceSoft
Demivec wrote:Code: Select all
;by value
Procedure test(*d)
Protected e.myStruct
CopyStructure(*d, e, myStruct)
Debug e\a
EndProcedure
test(aa.myStruct)
This example works only for own written procedures. Not for external dll which want be wrapped for PB.
Re: structures as parameters by value (not reference)
Posted: Sun Aug 15, 2010 1:36 am
by Demivec
IceSoft wrote:This example works only for own written procedures. Not for external dll which want be wrapped for PB.
@IceSoft: You're correct, I had forgotten that you wanted this specifically for PureBasic wrappers.

Re: structures as parameters (ByValue)
Posted: Sun Aug 15, 2010 10:45 am
by cas
If you want to communicate with external dll then you must use AllocateMemory()/FreeMemory().
Code: Select all
Structure test
x.i
y.i
EndStructure
Procedure create()
Protected *dat.test=AllocateMemory(SizeOf(test))
*dat\x=15
*dat\y=45
ProcedureReturn *dat
EndProcedure
Procedure test_separate_copy(*struct)
Protected privatecopy.test
CopyMemory(*struct,@privatecopy,SizeOf(test))
Debug privatecopy\x
Debug privatecopy\y
EndProcedure
Procedure test(*struct.test)
Debug *struct\x
Debug *struct\y
*struct\y=80
EndProcedure
Define *mystruct.test=create()
test(*mystruct)
Debug *mystruct\x
Debug *mystruct\y
test_separate_copy(*mystruct)
FreeMemory(*mystruct)
Re: structures as parameters (ByValue)
Posted: Sun Aug 15, 2010 11:46 am
by IceSoft
@cas,
As long you use *<varaiable name> as parameter as long it is always a pointer and this is byReference not byValue.
Re: structures as parameters (ByValue)
Posted: Sun Aug 15, 2010 12:13 pm
by cas
Yes, but when you have reference then you know value to which it points, so i don't see a problem. Can you explain more how do you want to behave test() procedure in your first post? Do you want it to make separate protected d.myStruct which, when modified inside procedure, will not make changes in aa.myStruct?
Or you wan't to use C++ dll in PureBasic when it returns structure like this one(?):
Code: Select all
#define EXPORT extern "C" __declspec(dllexport)
struct DataStructure
{
int x;
int y;
};
EXPORT DataStructure TestReturnStructure() {
DataStructure data;
data.x=4;
data.y=7;
return data;
}
Re: structures as parameters (ByValue)
Posted: Mon Aug 16, 2010 9:28 pm
by OrangeJuice
I think there was a good reason why C didn't allow that which i can't seem to remember right now. Maybe that is a slow thing to do?