1

I want to populate an array of records, and with my recent exposure to TMappedFile, I am trying to duplicate it, w/o actually using unFileMapping or BlockRead/Write. The purpose is to leave my options open just for that. The problem I am finding is either its not initiating or Create'ing my array correctly.

From what I have seen so far, is that the DataArr remains nil after compiling. And causes an memory violation error (0x00000000) which means it's empty/null. My engineer has already addressed to me that the size of the array maybe to large for purpose, and has also mentioned that I am not able to do what I want unless I deference the pointer, ex. DataArr^[0].value1

I believe my confusion lies with SetLength which seems counter intuitive for a Dynamic Array, which is why I commented it out. Otherwise I get an error E2008 incompatible types.

type
  TDataStruct = Record
  value1 : array[0..1] of Single;
  value2 : array[0..1] of Single;
  value3 : array[0..1] of Single;
  end;

  TDataStructArray = array[0..MaxInt div SizeOf(TDataStruct) - 1] of TDataStruct;
  PDataStructArray = ^TDataStructArray;

var
  DataArr: PDataStructArray;

begin
  //SetLength(DataArr,length(DataArr)+1);
  DataArr[0].value1[0] := 2;

Other things that I have tried is used FillChar(DataArr,SizeOf(DataArr),0) and as well as FillChar(DataArr[0], SizeOf(DataArr[0]) * Length(DataArr));

The following links have been my sources

  1. How to delete an Index of the Dynamic Array of Records
  2. Using New/Dispose with record pointer containing WideString
  3. Assigning a pointer of a record to an array of char in Delphi - come again?
  4. How do i declare an array when i don't know the length until run time
Community
  • 1
  • 1
Barry Dick
  • 161
  • 3
  • 16
  • 1
    Why aren't you using dynamic arrays? – David Heffernan Oct 13 '16 at 18:18
  • The problem is that the pointer is not pointing to anywhere. To populate an array of records, first you have to have an array of records, you have none. – Sertac Akyuz Oct 13 '16 at 18:18
  • As others have suggested, you should use a dynamic array. If you want to use a pointer to an array as you have then you need to get memory for that array before you use it and free the memory afterwards. e.g. for 5 records in the array you would do: `GetMem(DataArr, SizeOf(TDataStruct) * 5);` `DataArr[0].value1[0] := 2;` `FreeMem(DataArr);` – Graymatter Oct 13 '16 at 18:34
  • You don't have any array so far, only the helper types. – Free Consulting Oct 13 '16 at 22:05

1 Answers1

9

A dynamic array is what is says: dynamic.

Dynamic arrays do not have a fixed size or length. Instead, memory for a dynamic array is reallocated when you assign a value to the array or pass it to the SetLength procedure.

Declare

DataArr: Array of TDataStruct;

Use SetLength to allocate records and initialize them at the same time.

SetLength(DataArr,Length(DataArr)+1); // Or preallocate as many as you wish to initialize

Access a record in the array:

DataArr[0].value1[0] := 2;

In your example there is no memory allocation made, hence the error. And SetLength operates on dynamic arrays, not on pointers to static arrays.

You could manage the pointer with GetMem/FreeMem and initialize with ZeroMem, but that will only cause troubles building and maintaining the code.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • Another option is to use a variable, to allocate memory for the static array I mean.. – Sertac Akyuz Oct 13 '16 at 19:28
  • @SertacAkyuz, from the look of the question, it seems that the size of the array is not known at compile time. – LU RD Oct 13 '16 at 19:35
  • I need to make a loop verification with this method, and everytime I do that, I receive a code violation. But if I add the [0..0] I get the error 'incompatible types' when I try to increase the array size. Do you know what can I be doing wrong? – riki481 Jul 27 '20 at 20:19
  • 1
    @riki481, hard to say why the dynamic array allocation fails without seeing your code. A static array is incompatible with a dynamic array and cannot be resized, hence the compiler error. – LU RD Jul 27 '20 at 22:53