[fpc-pascal] 2 Q on unexpected behaviour of FPC2.0.4

Hans MÃ¥rtensson cirkulation at maartensson.net
Sun Dec 17 22:47:27 CET 2006

When making some windows program with FPC2.0.4 using sysutils, I 
encountered a few problems that I tracked down to some (by me) 
unexpected behaviour of the compiler. The documentation is not clear on 
these points. So may I ask if the following is intentional or rather 
some bug.

When trying to open a file that was not there with this program:
program test;
uses sysutils;
var n: longint; f: THandle; buf: array[0..100] of byte;
  f:=FileOpen('notafile', fmOpenRead);
  n:=FileRead(f, buf, 50);

The file handle was returned as some positive value, while n was 
returned -1.
I would expect the file handle to be returned as -1 (when file is not 
Otherwise there would be no meaning of testing the file handle?

When using a dynamic array a in the program line
FileWrite(f, a, bytecount)
I had unexpected data written to the file, while the line
FileWrite(f, a[0], bytecount)
did what I wanted.

That may be logical, if a is a pointer, while a[0] is the first element 
of the array.
But it is different from static arrays, where a acts as the first 
element of the array.
Also sizeof(a) returns the full size of the array with a static array, 
but always 4 with a dynamic array.
If this is intentional, couldn't it be stated in the documentation?

Finally I would like to know:
What happens to a dynamic array if it is resized?
Are the old data copied into a new larger space of memory, and the old 
memory freed, such that the elements of the array are still contiguous 
in memory?

Hans Maartensson

More information about the fpc-pascal mailing list