[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: varptr to structs returns bad pointer value


Le 27/02/2025 à 21:07, Brian G a écrit :
I am not sure if this is a bug or not

When passing structures to external c functions varptr fails to produce the correct pointer... to the data of the structure. I think maybe I don't understand the difference, but it would seem they should
produce the same result.

extern myFunction(data as pointer) use "mylib"
public dataSet as pointer
public struct myStruct
     result as long
     msg as string
end struct
public mydata as mystruct

this works as expected

public sub doit()
   dataSet = alloc(sizeof(mystruct))
   mydata = dataSet
   myFunction(dataSet)  ' this works
end sub

this fails

public sub doit2()
   mydata = new myStruct
   dataSet = varptr(myData)
   myFunction(dataSet)  ' this fails invalid pointer provided
end sub


VarPtr() tries to return the memory address of where the value of a variable is stored.

So when you write:

dataSet = varptr(myData)

you get the the address of where the value of 'mydata' is stored, not the address of the 'new MyStruct' object or the address of its contents.

To get the address of a Gambas object contents, you must use the 'Object.Data()' static method on the object reference.

dataSet = Object.Data(myData)

Regards,

--
Benoît Minisini.


Follow-Ups:
Re: varptr to structs returns bad pointer valueJussi Lahtinen <jussi.lahtinen@xxxxxxxxx>