www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Force struct allocation on the heap?

reply Benji Smith <dlanguage benjismith.net> writes:
Looking at the official docs, I can't find the D1 syntax for forcing a 
struct to be allocated on the heap (so that it can safely be returned 
from a function). I'm pretty sure it's possible (without wrapping the 
struct in a class or an array), but I can't for the life of me remember 
how it's done.

Thanks for your help!

--benji
Oct 18 2008
next sibling parent John Reimer <terminal.node gmail.com> writes:
Hello Benji,

 Looking at the official docs, I can't find the D1 syntax for forcing a
 struct to be allocated on the heap (so that it can safely be returned
 from a function). I'm pretty sure it's possible (without wrapping the
 struct in a class or an array), but I can't for the life of me
 remember how it's done.
 
 Thanks for your help!
 
 --benji
 

Doesn't this work? ----------------------------- struct sample { int a; int b; } auto s = new sample; ----------------------------- or even the long way, following the example of D custom class allocators (from D language manual): ---------------------------- struct sample { int a; int b; } sample* p; p = std.c.stdlib.malloc(sample.sizeof); if (!p) throw new OutOfMemoryException(); std.gc.addRange(p, p + sample.sizeof); ---------------------------------------- -JJR
Oct 18 2008
prev sibling next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Sun, Oct 19, 2008 at 12:25 AM, Benji Smith <dlanguage benjismith.net> wrote:
 Looking at the official docs, I can't find the D1 syntax for forcing a
 struct to be allocated on the heap (so that it can safely be returned from a
 function). I'm pretty sure it's possible (without wrapping the struct in a
 class or an array), but I can't for the life of me remember how it's done.

..you can always safely return a struct from a function, unless of course it has members that point onto the stack.
Oct 18 2008
parent Benji Smith <dlanguage benjismith.net> writes:
Jarrett Billingsley wrote:
 On Sun, Oct 19, 2008 at 12:25 AM, Benji Smith <dlanguage benjismith.net> wrote:
 Looking at the official docs, I can't find the D1 syntax for forcing a
 struct to be allocated on the heap (so that it can safely be returned from a
 function). I'm pretty sure it's possible (without wrapping the struct in a
 class or an array), but I can't for the life of me remember how it's done.

..you can always safely return a struct from a function, unless of course it has members that point onto the stack.

Oops. I meant to say that I need to return a struct *POINTER* from a function. In which case, I think I need to force heap allocation. Thanks! --benji
Oct 19 2008
prev sibling parent reply Lars Kyllingstad <public kyllingen.NOSPAMnet> writes:
Benji Smith wrote:
 Looking at the official docs, I can't find the D1 syntax for forcing a 
 struct to be allocated on the heap (so that it can safely be returned 
 from a function). I'm pretty sure it's possible (without wrapping the 
 struct in a class or an array), but I can't for the life of me remember 
 how it's done.
 
 Thanks for your help!
 
 --benji

You allocate a struct on the heap using the "new" keyword. struct Foo { ... } Foo* foo = new Foo; Note that "new" returns a pointer for non-object types. -Lars
Oct 19 2008
parent Benji Smith <dlanguage benjismith.net> writes:
Lars Kyllingstad wrote:
 You allocate a struct on the heap using the "new" keyword.
 
   struct Foo { ... }
   Foo* foo = new Foo;
 
 Note that "new" returns a pointer for non-object types.

Perfect. Thanks! --benji
Oct 19 2008