[fpc-devel] "dynamic array of char" vs. "out array of char"

Seth Grover sethdgrover at gmail.com
Tue Apr 27 23:21:43 CEST 2010


Observe the following test program (http://pastebin.org/186091 for
those who prefer):

============================================================
program dynarrayout;

{$mode objfpc}{$H+}

uses
  SysUtils;

  procedure DoItVar(var yourArray : array of char);
  begin
  end;

  procedure DoItOut(out yourArray : array of char);
  begin
  end;

type
  DynCharArray = array of char;

var
  myArray : DynCharArray;
  myArray2 : DynCharArray;
begin
  SetLength(myArray, 64);
  try
    FillChar(myArray[0], Length(myArray), '!');
    DoItVar(myArray);
    writeln('DoItVar is ok');
  except
    on E : Exception do begin
      writeln('Exception calling DoItVar: ', E.Message);
    end;
  end;
  SetLength(myArray, 0);

  SetLength(myArray2, 64);
  try
    FillChar(myArray2[0], Length(myArray2), '~');
    DoItOut(myArray2);
    writeln('DoItOut is ok');
  except
    on E : Exception do begin
      writeln('Exception calling DoItOut: ', E.Message);
    end;
  end;
  SetLength(myArray2, 64);
end.
============================================================

So basically I have a type defined as a dynamic array of characters,
which I fill with some data. If I have a function which takes a
parameter which is a "var array of char" into which I pass a variable
of my dynamic array type it works fine. However, if I have a function
where the parameter is an "out array of char", I get a crash when I
try to pass in my variable.

Is this behavior correct? Can someone explain this to me? I thought
that the only difference between "var" and "out" was whether or not
the compiler warned you about uninitialized data.

Thanks,

-SG

--
This email is fiction. Any resemblance to actual events
or persons living or dead is purely coincidental.

Seth Grover
sethdgrover[at]gmail[dot]com



More information about the fpc-devel mailing list