digitalmars.D.learn - Force struct allocation on the heap?
- Benji Smith <dlanguage benjismith.net> Oct 18 2008
- John Reimer <terminal.node gmail.com> Oct 18 2008
- "Jarrett Billingsley" <jarrett.billingsley gmail.com> Oct 18 2008
- Benji Smith <dlanguage benjismith.net> Oct 19 2008
- Lars Kyllingstad <public kyllingen.NOSPAMnet> Oct 19 2008
- Benji Smith <dlanguage benjismith.net> Oct 19 2008
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! --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
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: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
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
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









John Reimer <terminal.node gmail.com> 