Copy a struct on the heap and get the pointer to it.

deadalnix deadalnix at gmail.com
Sat Nov 12 06:39:38 PST 2011


Hi all,

We recently had a discution on #D about copying a struct on the heap and 
getting a pointer to it.

We come up with the following solution (Thanks to CyberShadow !) :

struct S;
S s; // Allocated locally.
[s].ptr; // Get a pointer to a copy in the heap of the struct

What is going on ? An array of one object is allocated on the heap and 
the struct copied in it. Then we get a pointer to the first (and only 
one) element of the array with ptr.

So we have a pointer to a copy of s on the heap.

I have several question on that :
  - Does a idiomatic way to do that exists ?
  - Is this intented to do so ? Will this construct risk to be broken in 
a future version of D ?
  - Is this construct the idiomatic way to do so ?


More information about the Digitalmars-d mailing list