An IDL array type is mapped directly to a C++ array type, and two other C++ types. The first of these types is an _var
type that can optionally be used to manage the storage of dynamically allocated arrays; the second is a slice type that is used for return values and for some out parameters.
Given the following IDL typedef
typedef long vector[10];
an instance of the array type can be defined in C++ as follows:
vector x1; // A normal C++ array (size 10).
An array can be dynamically allocated using either of the following two statements:
long* p1, = vector-alloc( ); // Size is 10, of course. vec10_var p2 = vector_alloc( ); // Size is 10, of course.
A C++ type, vector_slice
, is generated as an alias for long; so the first of these statements can also be written as
vector_slice* p1, = vector_alloc( ); // Size is 10, of course.
The difference is that the sequence pointed to by p1 must be deallocated explicitly with vector_free p1
, whereas the sequence pointed to by p2
is deallocated automatically when p2
goes out of scope. The functions vector_alloc( )
and vector_free ( )
are generated by the IDL compiler, and they must be used to allocate and deallocate arrays, respectively.
The type vect10_var
defines operator[ ]
to allow easy access to the underlying array. Hence, statements such as the following are valid:
p2[4] = p1[7];
When an array is passed as a parameter to an operation or appears as the return type, then the corresponding C++ formal parameter types are in shown as follows.
IDL type | in | out | inout | return |
---|---|---|---|---|
Array A of fixed-sized elements |
const A |
A |
A |
A_slice* |
Array A of variable-sized elements |
const A |
A_slice*& |
A |
A_slice* |
Conventionally in C++, a function that returns an array of elements is written to return a pointer to the first element or row:
long* func( );
This convention is followed by the IDL to C++ mapping. For example:
typedef long vector[10] interface T { vector op1(in vector a); }
maps to:
typedef CORBA::Long vector[10]; typedef CORBA::Long vector_slice; public:class T : public virtual CORBA::Object { vector-slice *op1 (const vector a); }
The memory management rules for IDL arrays are based on those for strings, pointers to an array of characters. The following differences should be noted: