Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi passing parameters by reference or by value/copy

Context 1

var text:String;

text:='hello';

myFunc(text);

Context2

function myFunc(mytext:String);
var textcopy:String;
begin

    textcopy:=mytext;

end;

myFunc on the Context2 was called from the Context1, the local variable mytext is pointing to a memory outside the Context2? or the mytext have have their own memory space inside the scope, and filled/copied with the same content of the text? I'm probably missing something really basic, because I'm getting a access violation error.

There's any way to specify explicitly if a function should receive parameters by reference or by value, copying then like in C? I'm not sure about how I'm doing it.

like image 692
Vitim.us Avatar asked Mar 03 '12 04:03

Vitim.us


People also ask

Is parameter passed by value or reference?

Java always passes parameter variables by value. Object variables in Java always point to the real object in the memory heap. A mutable object's value can be changed when it is passed to a method.

Which is faster pass by value or pass by reference?

As a rule of thumb, passing by reference or pointer is typically faster than passing by value, if the amount of data passed by value is larger than the size of a pointer.

How do you pass by reference in Delphi?

In Delphi to pass by reference you explicitly add the var keyword: procedure myFunc(var mytext:String); This means that myFunc can modify the contents of the string and have the caller see the changes.

Does pass by value make a copy?

pass-by-value makes a shallow-copy of the object. On the other side, pass-by-reference does not make any copy, it gets the reference of the object itself by just renaming it, so no any copying operation.


3 Answers

Memory management for Delphi strings is a little unusual. After you call myFunc(text), and assign textcopy := mytext, all three variables (text, mytext and textcopy) will be pointing to the same address, that of the original string.

But as soon as you use one of these variables to make changes to the string, Delphi clones the string behind the scenes, and your changes are applied to the copy. The other two variables still point to the original, so they remain unchanged. So any changes made in Context 2 will not be seen in Context 1 - this "copy-on-write" mechanic effectively gives you pass-by-value semantics. All of these strings are reference-counted, and will be freed automatically once all references go out of scope.

However, there is an exception. If you access the string using pointers, rather than string operations, you'll bypass the copying step and your changes will affect the original. You'll also bypass the reference counting logic, and potentially end up with a pointer to a deallocated block of memory. This may be the reason behind your access violation, but I couldn't say without more details / more code.

If you want reference passing, declare your function as myFunc(var mytext: String). If you want to force Delphi to copy the string, instead of waiting until it's modified, you can use System.UniqueString.

like image 67
Nick Barnes Avatar answered Oct 02 '22 02:10

Nick Barnes


In Delphi, string is a reference type that normally acts like a value type. It is allocated on the heap (not the stack like most value types) and features automatic reference counting and copy-on-write semantics.

To understand what this means, consider how normal value types, e.g. an Integer, behave when passed as a parameter to a procedure:

var
  gValue: Integer;

procedure PassByValue(aValue: Integer);
begin
  // Here @gValue <> @aValue
  aValue := aValue + 2;
  // Here @gValue <> @aValue
end;

procedure PassByRefrenceInOut(var aValue: Integer);
begin
  // Here @gValue = @aValue
  aValue := aValue + 2;
  // Here @gValue = @aValue
end;

procedure CallProcedures;
begin
  gValue := 0; 
  PassByValue(gValue);
  // gValue is still 0
  PassByReferenceInOut(gValue);
  // gValue is 2
end;

The var parameter in PassByReferenceInOut is equivalent to the C convention of passing a pointer to the argument.

The same semantics apply to string parameter passing, but there is a subtle difference in the internal representation of the values:

var
  gValue: string;

procedure PassByValue(aValue: string);
begin
  // Here PChar(gValue) = PChar(aValue) <<<<
  aValue := aValue + '2';
  // Here PChar(gValue) <> PChar(aValue)
end;

procedure PassByRefrenceInOut(var aValue: string);
begin
  // Here PChar(gValue) = PChar(aValue)
  aValue := aValue + '2';
  // Here PChar(gValue) = PChar(aValue)
end;

procedure CallProcedures;
begin
  gValue := ''; 
  PassByValue(gValue);
  // gValue is still ''
  PassByReferenceInOut(gValue);
  // gValue is '2'
end;

If you want to make sure a procedure operates on its own copy of the string value, use the UniqueString procedure, e.g.:

procedure PassByValue(aValue: string);
begin
  // Here PChar(gValue) = PChar(aValue)
  UniqueString(aValue);
  // Here PChar(gValue) <> PChar(aValue)
  aValue := aValue + '2';
  // Here PChar(gValue) <> PChar(aValue)
end;
like image 31
Henrick Hellström Avatar answered Oct 02 '22 01:10

Henrick Hellström


In Delphi to pass by reference you explicitly add the var keyword:

procedure myFunc(var mytext:String);

This means that myFunc can modify the contents of the string and have the caller see the changes.

like image 29
Mike W Avatar answered Oct 02 '22 01:10

Mike W