I am working on a toy language that uses C++ as an intermediate language, currently it only supports three types all decent from a base class, integer, list and lambda. All functions pass base class back and forth. Compiled code runs on a microcontoller, which means there are certain restrictions I only have 8 kb of ram so ideally I want to get rid of the object as soon as I am done with it. Also I don’t have access to most of the standart libs (no Boost, STL etc..).
So my question is how should i attack this problem? When I started I though I would just use shared pointers but that turns out does not really work say when a bunch of integers are added to a list.
I know of two general mechanisms for garbage collection:
There are various flavors / improvements which correspond more to implementations strategies (Generational, Copying, Compacting, …).
In general, the Reference Counting is the best for reactivity (which is important here), however there is the issue of reference cycles (depending on your toy language semantics).
There are complicated algorithms to deal with the collection of cycle, but the simpler solution is: