Reuse C memory for D struct?
Ali Çehreli
acehreli at yahoo.com
Thu Nov 21 11:21:10 PST 2013
On 11/21/2013 07:22 AM, Lemonfiend wrote:
> I'm wondering if it's possible to have a struct in D which uses the same
> pointer and memory as returned by the extern C function.
> This would allow me to manipulate and use the C struct directly in D
code.
>
> I'm not sure how to better explain this, hopefully the following
> pseudocode clarifies my question
>
> struct Tree
> {
> enum treeSize = 40;
>
> ubyte[treeSize] _this;
>
> this(int apples)
> {
> // use the C provided pointer somehow?
> // neither of the following seem to do the trick
> //this = *cppNew(apples);
> //_this = *cast(ubyte*)cppNew(apples)
If you instead maintain a slice of Apples and assuming that 'apples' is
the number of apples, then you can use the following syntax
Apple[] _this;
// ...
_this = cppNew[0 .. apples];
I have some information about that syntax under the "Producing a slice
from a pointer" section here:
http://ddili.org/ders/d.en/pointers.html
One thing that is not mentioned in there is that you are still
responsible for the Apples that are returned by the C library.
Ali
More information about the Digitalmars-d-learn
mailing list