The creator allocates memory for an object, calls the initializer for the object, and returns a pointer to the object created.
For example, the following creator is generated for the object A:
A * A_Create() {
A* me = (A *) malloc(sizeof(A));
if(me!=NULL)
{
A_Init(me);
}
return me;
}
For reactive objects, a pointer to a task is added to the (end of the) argument list for the creator. This pointer tells the reactive object which thread (task) it is running on. For example:
A * A_Create(RiCTask * p_task) {
A* me = (A *) malloc(sizeof(A));
if(me!=NULL)
{
A_Init(me, p_task);
}
DYNAMICALLY_ALLOCATED(me);
return me;
}
Because in C it is not possible to give an argument a default value, you can pass a value of NULL for the task to cause the instance to run in the main task.
The C_CG::Class::AllocateMemory property and the C_CG::Event::AllocateMemory property specify the string generated to allocate memory dynamically for objects or events. This string is used in the Create() operation. The default value of this property is:
($cname*) malloc(sizeof($cname));
In generated code, the $cname keyword is replaced with the name of the object or event for which memory is being allocated.
You can create an object dynamically by calling its creator function. For example:
B *new_B;
new_B = B_create();
You can delete an object dynamically by calling its delete function. For example:
B_Destroy(new_B);