Reuse C memory for D struct?

Lemonfiend lemon at fie.nd
Thu Nov 21 07:22:09 PST 2013


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);
}


More information about the Digitalmars-d-learn mailing list