www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.learn - classes allocated on the stack

↑ ↓ ← "Steven Schveighoffer" <schveiguy yahoo.com> writes:
Regarding this documentation 
(http://www.digitalmars.com/d/1.0/memory.html#stackclass):

<quote>
Class instances are normally allocated on the garbage collected heap. 
However, if they:

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

then they are allocated on the stack.
</quote>

Why does rule 3 exist?  If I have a class:

class X
{
    private int n;
    this()
    { n = 5; }

    this(int x)
    { initialize(x); }

    initialize(int x)
    { n = x; }
}

And I want to allocate an instance on the stack, with n initialized to 3:

scope x = new X; // allocated on stack
x.initialize(3);
scope x2 = new X(3); // allocated on heap?

What about using the constructor with parameters makes it not possible to 
store on the stack?

-Steve 
Apr 24 2008
↑ ↓ Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Steven Schveighoffer wrote:
     * use new with no arguments

 Why does rule 3 exist?  If I have a class:

 
 What about using the constructor with parameters makes it not possible to 
 store on the stack?

"New with arguments" != "constructor with parameters". I think you missed this part of the spec: <http://www.digitalmars.com/d/1.0/class.html#ClassAllocator>
Apr 24 2008
↑ ↓ → "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Frits van Bommel" wrote
 Steven Schveighoffer wrote:
     * use new with no arguments

 Why does rule 3 exist?  If I have a class:

 What about using the constructor with parameters makes it not possible to 
 store on the stack?

"New with arguments" != "constructor with parameters". I think you missed this part of the spec: <http://www.digitalmars.com/d/1.0/class.html#ClassAllocator>

Ah yes, I hadn't thought of it meaning that. Another case of the example not being explicit enough :) It would be nice if the counter example (with an example of new with arguments) was listed. I'll post an enhancement report. Thanks! -Steve
Apr 24 2008