On Thursday, 21 November 2013 at 15:22:10 UTC, Lemonfiend wrote:
Hi!

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)
        }
        
        Tree* cppNew(int apples)
        {
// calls extern C constructor which returns a pointer to a Tree of size treeSize
        }
        
        ~this()
        {
                cppDelete();
        }
        
        void cppDelete()
        {
// this needs the pointer returned by cppNew to do an extern C delete
        }
}

void main()
{
        Tree tree = Tree(12);
}

if your D struct is POD and alocated on the heap, it's the same, as long as data type are the same and not plateform-dependent.I'm mean that that you just cast from the initial pointer...

Reply via email to