0

I'm working on a solution that should work in C++-Builder and Delphi, that's why I use Object Pascal syntax, but I'm not very familiar with it. I try to access a file mapping with a size that is not defined at runtime with Free Pascal/Delphi. I tried

type
  TFileMappingLayout = packed record
    Size: DWORD;
    Data: array [0..0] of byte;
  end;
  PFileMappingLayout = ^TFileMappingLayout;

but I'm not sure if this is conform with range checking. It seems to be impossible to google for it, I found nothing useful so far. Reading descriptions about creating structured types did not mention cases like this. I also had a look into the Lazarus source code, but I gave up after 100 trivial record definitions...

I use the Data field only for binary copying, for instance, writing to it:

  PFileMappingLayout(FData)^.size := cbData;
  Move(myData, PFileMappingLayout(FData)^.Data, cbData);

How is this normally done in Delphi/Free Pascal?

How would you name this kind of record definition (open record, partial record)?

Kilian Foth
  • 110,899
Wolf
  • 640

1 Answers1

1

To avoid range-check errors, you should rather define the data array as follows:

Data: array[0..MaxInt-1] of Byte;

In general, for a type TMyType, you can define such an open-ended array as follows:

Data: array[0..MaxInt div SizeOf(TMyType) - 1] of TMyType;

But this is all if you specifically want to have a record structure as you currently have. Personally I would just use a dynamic array:

Data: array of Byte;

Use SetLength() to initialize its count and Length() to get its current count. You don't have to allocate or de-allocate any memory for this as you would have to for your implementation.

(Sorry, I don't know what they call such record types as you indicated...)