www.digitalmars.com         C & C++   DMDScript  

D - Clarification of stack instance memory allocation, feature proposal

reply "Jeroen van Bemmel" <anonymous somewhere.com> writes:
Walter,

If I read the D language specification correctly then class instances can
never be on the stack, since there is an implicit reference. So just like in
Java, you need to do:

void f()
{
    ClassX x = new ClassX();
}

??

This is one of the features of Java I do not like so much, you have to
allocate so much memory all the time.

I assume you could use 'auto' to indicate to the compiler/runtime that x
should be deallocated (and destructor called) once it goes out of scope.
Would it be possible, perhaps as an optimization, that in such a case the
compiler uses 'alloca' to reserve memory for x (from the stack) ?

Is the same true for structs ?
Jan 25 2003
parent "Walter" <walter digitalmars.com> writes:
"Jeroen van Bemmel" <anonymous somewhere.com> wrote in message
news:b0v8lq$2vck$1 digitaldaemon.com...
 If I read the D language specification correctly then class instances can
 never be on the stack, since there is an implicit reference. So just like

 Java, you need to do:

 void f()
 {
     ClassX x = new ClassX();
 }

 ??

 This is one of the features of Java I do not like so much, you have to
 allocate so much memory all the time.

You're correct in that class objects can only be allocated on the heap. However, arrays and structs can be allocated on the stack. Far less memory is needed than for Java.
 I assume you could use 'auto' to indicate to the compiler/runtime that x
 should be deallocated (and destructor called) once it goes out of scope.
 Would it be possible, perhaps as an optimization, that in such a case the
 compiler uses 'alloca' to reserve memory for x (from the stack) ?

It's a good idea, but I'd have to think it through.
 Is the same true for structs ?

No. Structs can be on the stack, in static memory, or on the heap.
Jan 25 2003