January 26, 2003 Clarification of stack instance memory allocation, feature proposal | ||||
---|---|---|---|---|
| ||||
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 ? |
January 26, 2003 Re: Clarification of stack instance memory allocation, feature proposal | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen van Bemmel | "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 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. 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. |
Copyright © 1999-2021 by the D Language Foundation