Solved

Delphi, Arrays returned by functions must be freed?

Posted on 2014-10-13
2
397 Views
Last Modified: 2014-10-13
Hello,
I've got a doubt about the need of freeing arrays returned by functions.
Here's my array type and function:

type
  TStringArray = array of string; 

function GetHelloStringArray() : TStringArray;
begin
     SetLength(Result,5);
     Result[0] := 'H';
     Result[1] := 'E';
     Result[2] := 'L';
     Result[3] := 'L';
     Result[4] := 'O';
end;

Open in new window


Suppose I'm using the GetHelloStringArray function as shown here:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyArray : TStringArray;
begin
  MyArray := GetHelloStringArray();
  //DO SOMETHING HERE
  //...
  SetLength(MyArray,0) // <--- IS THIS NEEDED?
end;

Open in new window


Is the SetLength command needed or can be avoided?
Thanks to everyone.
0
Comment
Question by:ExDev001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 40376707
Ask the oracle itself (Delphi) :)

program Project9;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  TStringArray = array of string;

function GetHelloStringArray(): TStringArray;
begin
  SetLength(Result, 5);
  Result[0] := 'H';
  Result[1] := 'E';
  Result[2] := 'L';
  Result[3] := 'L';
  Result[4] := 'O';
end;

var
  MyArray: TStringArray;

begin
  ReportMemoryLeaksOnShutdown := True;
  MyArray := GetHelloStringArray();
  WriteLn('Press any key...');
  ReadLn;

end.

Open in new window


MyArray is a normal data type, thus you don't need to do anything. I would be different, when this is a reference type, where only the reference is managed by Delphi, but not the object or memory where the references points to.
0
 

Author Closing Comment

by:ExDev001
ID: 40377212
Thanks alot for clarification!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question