Dynamic array parameter

Top  Previous  Next

What is translated > Types > Arrays > Array parameters > Dynamic array parameter

A Delphi function accepts a dynamic array as parameter, if it is defined explicitly:

 

type

strarray = Array of String;

procedure CheckDynamicArray(aSources : strarray);

->

 

C++Builder

typedef DynamicArray<System::String> strarray;

void __fastcall CheckDynamicArray(strarray& aSources);

 

Other   

typedef std::vector<System::String> strarray;

void CheckDynamicArray(strarray& aSources);  

 

 

In this case Delphi2Cpp translates such a parameter as a reference to a dynamic array.

 

 

 

Let's compare this case with the case, where the called function has an open array parameter.

 

 
Other

 

For Other compilers than C++Builder there is no surprise, because the expected parameter is the same for CheckDynamicArray and CheckOpenArray:

 

procedure CheckOpenArray(const AArray: Array of String);

->

void CheckOpenArray(const std::vector<String>& AArray)

 

std::vector<String> strarray;

CheckDynamicArray(strarray);

CheckOpenArray(strarray);

 

 

C++Builder

 

For C++Builder however, though the calls of these function look similar in Delphi they lo quite different in C++:

 

procedure CheckOpenArray(const AArray: Array of String);

->

void __fascallCheckOpenArray(const String* AArray, int AArray_maxidx)

 

DynamicArray<String> strarray;

CheckDynamicArray(strarray);

CheckOpenArray(DynamicArrayPointer(strarray), strarray.High);

 

Instead of only one parameter here a pointer to the array is passed as a first parameter and the upper bound (High) of the array is passed as second parameter. The pointer to the array is calculated by the DynamicArrayPointer function, which returns the NULL pointer, if the array is empty.

 

template <class T> 

const T* DynamicArrayPointer(const DynamicArray<T>& DA, unsigned int Index = 0)

{

       if(DA.Length > 0)

               return &DA[Index];

        else

               return NULL;        

}

 

 

 

This function has to be used, because passing "&strarray[0]" throws an exception, when strarray is empty.

 

 

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content