Managed memory is allocated dynamicly and is hold in managed pointers. Every assigment to such a pointer is tracked. When no pointer refers to the managed memory, it is released. It is done automaticly, you needn't take care of memory deallocation.
In scripts managed pointers are created on the stack, their lifetime is the same as other other stack variables. When they are destructed they release the pointer to the memory they reference. They may be created also in the host code. Managed pointers are initialized to NULL at construction time.
Memory is allocated with the new operator. There are two versions of it and their syntax is the same as C++ new.
You can also define your own managed memory allocation handler, by passing the function pointer while environment initialization (in ISCEnvDesc structure). By default managed memory is allocated on the heap (C++ new).
This example shows some SC code that uses managed memory.
Vec2f ^p; //default set to NULL Vec2f ^v = new Vec2f(10, 10); //parametrized constructor Vec2f ^y = new Vec2f(); //default constructor (may be also new Vec2f;) { Vec2f ^v2 = v; //v2 refers to the memory pointed by v v = NULL; //v is null now } //v2 destroyed - destructor for memory pointed by v2 called since no other references y = v; //initial memory of y deleted
The code snippet below shows how to create a managed pointer on the host side. Managed pointers are represented by TSCPtr template with the pointed type as a template argument. It may be also a managed pointer (like below), but you cannot use native pointers here - no mixing possible. To allocate some memory use SC_ALLOC - if the pointer and the allocated type are the same, or SC_ALLOC_DERIVED if you alloc a derived class and assign to the base class pointer. This example is based on the memory tutorial, browse it to see used classes.
// array of 4 managed pointers - parameters: [pointer type], [number of elements] [namespace to search type, NULL for global] TSCPtr<< TSCPtr<Soldier> > soldiers = SC_ALLOC(TSCPtr<Soldier>, 4, NULL); //first allocation with the same type as soldier - SC_ALLOC macro soldiers[0] = SC_ALLOC(Soldier, 1, NULL); //to alloc derived class use SC_ALLOC_DERIVED macro - parameters: [pointer type], [allocated type], [number of elements] [namespace to search type, NULL for global] soldiers[2] = SC_ALLOC_DERIVED(Soldier, Marine, 1, NULL);
You can convert a managed pointer to native by getting it address (& operator returs the address of the pointed memory). It is not possible to convert a native pointer to mananged one.
You can create mananged multidimensional arrays. They are similar to Java arrays, or C++ array of pointers. One dimensional arrays is just a memory that contains objects (like C++ array). But a multidimensional array is an array of mananged pointers, not native. Each dimension may be set individually. See examples below.
Int ^^a = new Int[20][10]; //arrays of 20 pointer , each one is allocated and contains 10 Int numbers a[0] = new Int[5]; //now the first row contains 5 Int numbers Int ^^b = new Int^[5]; //allocated only array of pointers - first dimension, all point to NULL