www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - question on allocation

reply Sai <Sai_member pathlink.com> writes:
In D, ignoring std.c.* api and overloading "new" operator, 
heap allocation can be done only by using "new" operator,
isn't it ? Declarations of form

Type var;

always creates variables on stack, even for array types ?

Only for classes above form creates references and not actual
objects, and for every other kind of types, above form of
declaration always creates the actual instance.

please correct me if I am wrong.
thanks
Sai  

PS: I read the documentation 'n' number of times, but some things
are still confusing me.
Aug 20 2004
parent Arcane Jill <Arcane_member pathlink.com> writes:
In article <cg5j9b$lnb$1 digitaldaemon.com>, Sai says...
In D, ignoring std.c.* api and overloading "new" operator, 
heap allocation can be done only by using "new" operator,
isn't it ? Declarations of form

Type var;

always creates variables on stack, even for array types ?

Only for classes above form creates references and not actual
objects, and for every other kind of types, above form of
declaration always creates the actual instance.

please correct me if I am wrong.
thanks

Fixed-size arrays get created on the stack. For all other array types, a reference (to an empty array) is created on the stack. The actual data is created on the heap as soon as you modify .length or assign (which you can do with new). Dereferencing a null array-reference is not an error - it is simply regarded as an empty array. For classes, a null reference is created on the stack, which you need to explicitly make non-null by assignment or by using new (at which point, the object itself is created on the heap.) Dereferencing a null class-reference is an error. Arcane Jill
Aug 20 2004