www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D2.0 Stack allocated classes

reply S. <S s.com> writes:
Per the memory management whitepaper, the following conditions are given for
allocating a class on the stack instead of the heap.

    * are allocated as local symbols in a function
    * are allocated using new
    * use new with no argument
    * have the scope storage class

That covers every way I know how to allocate a class in D.  Maybe is poorly
written, and I'm confused.  Lists like this are typically used to imply that
ANY of the conditions being true would cause a stack allocation.  If that's not
the case, just say:

"If the class is allocated as a local symbol in a function using new with no
arguments and has the scope storage class, then it is allocated on the stack."

If I'm not confused, then how do you get something to be allocated on the heap?
Jun 29 2007
next sibling parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:
S. wrote:
 Per the memory management whitepaper, the following conditions are
 given for allocating a class on the stack instead of the heap.
 
The four bullet points must all be true.
 * are allocated as local symbols in a function
As opposed to members of some structure or class, or as global symbols, or the like.
 * are allocated using new
As opposed to e.g. returned from a function.
 * use new with no argument
If a class overloads the allocator, you can pass additional arguments to new itself if the allocator accepts them, e.g.: scope Foo f = new(1, 2, 3) Foo; If you use this feature, it won't go on the stack.
 * have the scope storage class
 
Obviously, this is as opposed to /not/ using the scope storage class.
 That covers every way I know how to allocate a class in D.  Maybe is
 poorly written, and I'm confused.  Lists like this are typically used
 to imply that ANY of the conditions being true would cause a stack
 allocation.
No, this list means that ALL of the conditions must be true.
  If that's not the case, just say:
 
 "If the class is allocated as a local symbol in a function using new
 with no arguments and has the scope storage class, then it is
 allocated on the stack."
 
It might be better to say "it may be allocated on the stack," or "the implementation is free to allocate it on the stack."
 If I'm not confused, then how do you get something to be allocated on
 the heap?
Foo f = new Foo; // on the heap -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Jun 29 2007
prev sibling next sibling parent Kyle Furlong <kylefurlong gmail.com> writes:
S. wrote:
 Per the memory management whitepaper, the following conditions are given for
allocating a class on the stack instead of the heap.
 
     * are allocated as local symbols in a function
     * are allocated using new
     * use new with no argument
     * have the scope storage class
 
 That covers every way I know how to allocate a class in D.  Maybe is poorly
written, and I'm confused.  Lists like this are typically used to imply that
ANY of the conditions being true would cause a stack allocation.  If that's not
the case, just say:
 
 "If the class is allocated as a local symbol in a function using new with no
arguments and has the scope storage class, then it is allocated on the stack."
 
 If I'm not confused, then how do you get something to be allocated on the heap?
I had the same cognitive error reading this, I'm not sure if its actually ambiguous, but it did trip me up. Perhaps explicitly state that all conditions must be true for stack allocation?
Jun 29 2007
prev sibling parent reply Jason House <jason.james.house gmail.com> writes:
S. wrote:
 Per the memory management whitepaper, 
Where can I find that?
Jun 30 2007
parent davidb <ta-nospam-zz gmx.at> writes:
Jason House schrieb:
 
 S. wrote:
 Per the memory management whitepaper, 
Where can I find that?
Under section "Articles" -> "Memory Management" on the left, http://www.digitalmars.com/d/memory.html david
Jun 30 2007