digitalmars.D.learn - Force struct allocation on the heap?
- Benji Smith (7/7) Oct 18 2008 Looking at the official docs, I can't find the D1 syntax for forcing a
- John Reimer (21/31) Oct 18 2008 Doesn't this work?
- Jarrett Billingsley (3/7) Oct 18 2008 ..you can always safely return a struct from a function, unless of
- Benji Smith (5/13) Oct 19 2008 Oops. I meant to say that I need to return a struct *POINTER* from a
- Lars Kyllingstad (6/15) Oct 19 2008 You allocate a struct on the heap using the "new" keyword.
- Benji Smith (3/9) Oct 19 2008 Perfect. Thanks!
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
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! --benjiDoesn'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
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
Jarrett Billingsley wrote:On Sun, Oct 19, 2008 at 12:25 AM, Benji Smith <dlanguage benjismith.net> wrote: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! --benjiLooking 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 19 2008
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! --benjiYou 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
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