Skip to content

Pointers

Pointers are a core feature of the C programming language. itemis CREATE’s Deep C/C++ Integration is making C pointers available to you in your statecharts. In particular, you can

  • declare typed pointer variables,
  • assign a pointer to a pointer variable of the same type,
  • retrieve the pointer pointing to a given variable,
  • pass pointers as parameters to functions, and
  • receive a pointer as a functions return value.

Declaring pointer variables

From version 5.1 Create supports the usage of smart pointers. They can be used externally e.g. in a header file or can be declared internally e.g. as the type of a statechart variable. As smart pointers got introduced in C++11 they are only supported with our C++11 code generator. Other C domain code generators will replace smart pointers with raw pointers. With this addition, some generator options got introduced too to enable the customization of which parts of the generated code the user wants to use smart pointers. For more detail on that please see GeneratorOptions feature .

Please be aware of that unique pointers may result in not functioning code out of the box as they require special handling regarding ownership.

Pointer variables are declared in a statechart’s definition section as shown in the following example:

var n: int32_t
var pInt: pointer<int32_t>
var spInt: shared_ptr<int32_t>
var ppInt: pointer<pointer<int32_t> >
var pTriangle: pointer<Triangle>;

The declarations above declare

  • n as a (non-pointer) variable of type int32_t,
  • pInt as a pointer variable that is pointing to a variable of type int32_t,
  • spInt as a smart pointer variable that is pointing to a variable of type int32_t,
  • ppInt as a pointer that is pointing to a pointer that is pointing to a variable of type int32_t, and
  • pTriangle as a pointer to a variable of the self-defined type Triangle.

Please note: When closing the type specification in a pointer declaration with angle brackets, e.g., pointer<pointer<int32_t> >, the > characters must be separated from each other by one or more white space characters. Writing, for example, pointer<pointer<int32_t>> would result in an error. This restriction will be fixed in a later release.

Using pointer variables

In order to actually assign a pointer to a pointer variable, you have to get hold of that pointer. To retrieve the pointer to a variable v, use v's extension function pointer. That is, for a variable v, the expression v.pointer evaluates to a pointer to v. Each variable has the pointer extension function.

From version 5.1 all variables also have the unique_ptr, shared_ptr, weak_ptr extension functions that will function just like the already existing pointer extension, only retrieving the corresponding smart pointer instead of raw pointer type.

Please be aware that unique pointers may result in not functioning code out of the box as they require special handling regarding ownership.

Example: Let’s say the pointer variable pInt (declared in the example above) should point to the variable n. The following assignment accomplishes this:

pInt = n.pointer

Similarly, a pointer to a pointer to a base type can be retrieved as follows:

ppInt = pInt.pointer;

Or even:

ppInt = n.pointer.pointer

In order to deference a pointer, i. e. to retrieve the value of what the pointer is pointing to, use the value extension function, which is available on all pointer-type variables.

Example: Let’s say the pointer variable pInt (declared in the example above) is pointing to some int32_t variable. The value of the variable pInt is pointing to should be assigned to the int32_t variable n. The following assignment accomplishes this:

n = pInt.value;

Similarly, if ppInt points to a pointer pointing to some int32_t variable, the following statement retrieves the latter’s value:

n = ppInt.value.value;

Passing pointer parameters to C functions is straightforward. Let’s say you have a C function to rotate a triangle around a center point by a given angle. The C function is defined like this:

Triangle* rotateTriangle(Triangle* triangle, Point* centerPoint, float angle) { … }

Provided the function is declared in an imported C header file, you can call it directly like this:

pTriangle2 = rotateTriangle(pTriangle, pCenterPoint, 45.0);

Please note: Assigning a pointer to a pointer variable is only possible if the pointer types are the same.