Ecere SDK/eC Forums • View topic - Class types and memory allocation

Ecere SDK/eC Forums

Ecere Developers Community

Class types and memory allocation

General help with the eC language.

Class types and memory allocation

Postby redj » Wed Feb 09, 2011 11:30 am

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++?

Posts: 107
Joined: Sun Jan 17, 2010 10:03 am

Re: Class types and memory allocation

Postby jerome » Wed Feb 09, 2011 2:26 pm

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 { };
Site Admin
Posts: 622
Joined: Sat Jan 16, 2010 11:16 pm

Re: Class types and memory allocation

Postby jfbilodeau » Wed Feb 09, 2011 2:33 pm

Are object reference count inc/dec manually or automatically?
Posts: 19
Joined: Wed Feb 09, 2011 11:47 am

Re: Class types and memory allocation

Postby jerome » Wed Feb 09, 2011 2:55 pm

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)
Site Admin
Posts: 622
Joined: Sat Jan 16, 2010 11:16 pm

Return to eC Language

Who is online

Users browsing this forum: No registered users and 1 guest