Ecere SDK/eC Forums
http://ec-lang.org/community/
Print view

Class types and memory allocation
http://ec-lang.org/community/viewtopic.php?f=1&t=126
Page 1 of 1
Author:  redj [ Wed Feb 09, 2011 11:30 am ]
Post subject:  Class types and memory allocation

Question from JF:
Beyond the type of class, Is there a way to choose if an instance of a class should reside on the stack of the heap when the class is instantiated, à la C++?

-redj
Author:  jerome [ Wed Feb 09, 2011 2:26 pm ]
Post subject:  Re: Class types and memory allocation

Short answer: not really.

A 'class' will ALWAYS be on the heap. The rationale for this is, constructing/destructing a class is a complex process, and the small overhead to allocate/free the memory is offset by that. One thing that's missing right now however is the auto-destruction of 'classes' when they go out of scope ( a reference decrement ). That will remove the need to 'delete' it, which might be why you're wishing for this? This category includes the 'class : struct' as well (Which, as a reminder, is missing the reference counting/virtual functions table/type information, and could (in theory, not sure if it's working right now) inherit from any eC or C 'struct').

A struct is always created 'in place' (automatic declaration I believe they call it).
That is just like a C++ object. The downside is you need to use the pointer notation (* and ->) with it if you want it to be on the heap. It will be on the heap if you allocate it with a new:
MyStruct * s = new MyStruct[1];

And on the stack if you just declare it:
MyStruct s;

You can also declare eC struct to be initialized with 0s by using the instantiation syntax:
MyStruct s { };
Author:  jfbilodeau [ Wed Feb 09, 2011 2:33 pm ]
Post subject:  Re: Class types and memory allocation

Are object reference count inc/dec manually or automatically?
Author:  jerome [ Wed Feb 09, 2011 2:55 pm ]
Post subject:  Re: Class types and memory allocation

Object reference counts are incremented automatically for 'member instances' (Instances declared using the instantiation syntax as data members of other classes). This is only possible inside 'classes' (not structs, as struct don't have a construction mechanism, only the possibility to be zero'ed out).
Member instances are similarly automatically decremented when their 'containing' object is destructed.

The other type of automatic reference counts increment/decrement are 'global' instances, again only when they are declared using the instantiation syntax:

MyClass object { }; // automatic reference counting
MyClass object; // Just a handle, didn't create anything yet, so not automatically reference counted

I plan to add the automatic reference count increment/decrement for blocks as well when you use the instantiation syntax. But for now, objects locally defined start with a 0 reference count and are neither incref'ed nor decref'ed.

To manually increase the refeference count, you simply do 'incref object;'.
To decrease it, you just use 'delete object;' (The object only actually gets deleted if its reference count goes down to 0, or lower).

(See also page 67 in the Tao)
All times are UTC-05:00 Page 1 of 1