Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi pass array by reference

In C++ is possible to pass to a function a vector by const reference in this way:

void test(const std::vector<int>& a);

This is useful when the vector is very big and I want to avoid the waste of time of making copy of it. I know that the same code in Delphi is:

procedure test(const [Ref] a: array of Integer);

Does it also have the same effect as C++ (pass the address instead of a copy and optimize/save time)? Is this the only way or there is also something else to optimize the parameter passing?

like image 313
Raffaele Rossi Avatar asked Dec 15 '16 08:12

Raffaele Rossi


2 Answers

procedure test(const a: array of Integer);

This is an open array, passed as const. These are already passed by reference. Adding [ref] in this situation is needless.

Only if you pass an open array by value will a copy will be made:

procedure test(a: array of Integer);

The other option, for sake of completeness, is to pass by var.

procedure test(var a: array of Integer);

Here the array is passed by reference, but, in contrast to the const array, the compiler permits its content to be modified.


I know that the same code in Delphi is ...

That's not quite accurate. Probably the best mapping from C++ std::vector<T> is to Delphi's TList<T>. Probably the closest match to a Delphi open array parameter would be a C++ array parameter. You might map your Delphi procedure:

procedure test(const a: array of Integer);

to this C++ function:

void test(const int a[], const size_t len);

So you aren't really comparing like for like.

That said, Delphi dynamic arrays, which you might well be using when you actually call such a function, are managed types. This means that their lifetimes are managed by automatic reference counting (ARC) and that sets them apart from raw C++ arrays.

I'm rambling somewhat now. Mostly what I am trying to get at is that the devil is in the details. None of these things map perfectly between these languages because the languages have their subtle differences.

But, leaving these nuances aside, if you wish to pass an array efficiently in Delphi, then a const open array will achieve that.

like image 134
David Heffernan Avatar answered Oct 21 '22 04:10

David Heffernan


You are confusing open array parameters and dynamic arrays. There is no need for the [Ref] here.

Open array parameters are actually passed as two parameters.

  • The first one contains the address of the first element of the array.
  • The second one the length of the array (number of elements) minus one, the so called High() value.

A vector in C++ is a class. It is passed like a class in Delphi, but constness is different. In Delphi, even if you pass a class as const, its methods can still be called. And in Delphi, class instances are references already. No need for the [Ref].

More info on open array parameters in my article.

like image 7
Rudy Velthuis Avatar answered Oct 21 '22 02:10

Rudy Velthuis