Maurice Lombardi wrote:
Adam Naumowicz wrote:
Thanks. I read the http://agnes.dida.physik.uni-essen.de/~gnu-pascal/gpc_37.html#SEC37 as the Chief suggested me to do, and now I can see what is the reason of such behaviour. But still I would like to allocate only a part of the array (to simulate a dynamic array - the piece of code I'm working with is from an old non-OOP Pascal version). I also cannot pass size to standard NEW call (at least in BP) ...
Then declare shortarry as an array of string pointers instead of an array of strings, and allocate with new the number of strings you really need.
AFAICS, this is the only way that will work with the same code for GPC and BP.
If you really want to avoid it, you could use a little conditional compilation (though I usually discourage that ;-), like this:
program test;
type NameStr=String[8];
{$ifdef __GPC__}
type ShortArray (Size: Integer) = array [1 .. Size] of NameStr; PShortArray = ^ShortArray;
function AllocShortArray (Size: Integer) = p: PShortArray; begin New (p, Size) end;
{$else}
type ShortArray = array [1 .. MaxInt div SizeOf (NameStr)] of NameStr; PShortArray = ^ShortArray;
function AllocShortArray (Size: Integer): PShortArray; var p: PShortArray; begin GetMem (p, Size * SizeOf (NameStr)); AllocShortArray := p end;
{$endif}
var n:NameStr; a:PShortArray; i:integer;
begin a := AllocShortArray (3); n:='12345678'; for i:=1 to 3 do begin a^[i]:=n; writeln('n = ',n); writeln('a^[i] = ',a^[i]); end; FreeMem (a, 3 * SizeOf (NameStr)) end.
You could write a conditional wrapper for FreeMem as well, but that's not absolutely necessary since GPC ignores the 2nd parameter, anyway (it knows by itself how big the memory allocated was).
Frank