[fpc-pascal] A warning when Blockwriting/reading dynamic array pointers

J├╝rgen Hestermann juergen.hestermann at gmx.de
Sat Apr 2 19:19:43 CEST 2011

DaWorm schrieb:
 > IMO, a dynamic array should never be part of a structure that is
 > passed to BlockWrite in the first place.  Not that I use many dynamic
 > arrays in the first place, but to me they pass over the border between
 > simple data types (that are fine for BlockWrite) and managed data
 > types (that have quite a lot of "gotchas").  I lump them in with
 > TObjects in that respect.

The problem with all these "managed data types" is, that their internal 
structure and behaviour is not fully documented which makes it hard to 
judge whether and how to use them.

I created a recursive record structure to represent a directory branch 
(files and directories) on disk:

type TreeListZType  = ^TreeListType;
     DirContentType = array of TreeListZTyp;
     TreeListType   = record
                      LastWriteTime : FileTime;
                      FileSizeLow : LongInt;
                      IsFile  : boolean;
                      DirContent : DirContentType;
                      Name  : AnsiString;
                      end; // of record

When storing all the data I Blockwrite each record except the last 
AnsiString (which I handle extra). When reading it back from file the 
dynamic array pointer "DirContent" serves as an automatic boolean 
because it tells me, whether the array was empty (=nil) or not. When not 
empty, I have to read back the number of elements of the array, 
otherwise nothing has to be read. Before doing so, I have to clear the 
old pointer so that SetLength works correctly. The problem was, that it 
never came into my mind that assigning nil will do more than just 
assigning a value. I did this just to prepare the pointer for the later 
use with SetLength.

More information about the fpc-pascal mailing list