title: Assigning a pointer of a record to an array of char in Delphi - come again?
body: <p>I recently started working on a Delphi project and found very strange piece of code.</p>
  
  <pre><code>// Type declarations
  
  type
      TDataSet = record
      VolumeLevel : char;
      DeviceMasks : char;
      DeviceList: array [1..MAX] of array [1..8] of char;
      DisplayList: array [1..MAX] of array [1..8] of char;
  end;
  
  type
      TSerialPacket = record
      Preamble: array[1..4] of byte;
      PacketType: byte;
      PacketLen: byte;
      Data: array of char;
      Checksum: byte;
  end;
  
  ...
  
  // Private fields
  
  Packet  : TSerialPacket;
  DataSet : TDataSet;
  
  ...
  
  // Actual procedure
  
  SetLength(Packet.Data, sizeof(DataSet));
  Packet.Data := @DataSet;
  </code></pre>
  
  <p>I haven't used Delphi very much so this code seems incomprehensible to me. Compiler thinks this is alright and I can run the code. I ran it with debugger but the value of Packet.Data does not seem to change. Can anyone tell me what this does? It seems very strange to assign a pointer of a custom record to an array of chars.</p>
  
  <p>Also, for some reason the SetLength triggers an error: "Project Foo.exe raised exception class EAccessViolation with message 'Access violation at address 00403860 in module 'Foo.exe'. Read of address 00000000.". During both the working and the crashing runs value of Packet.Data is () and sizeof DataSet is 260. I haven't been able to pinpoint what exactly changes. As far as I know, SetLength should not depend on any other variables than Packet.Data and DataSet.</p>
  
  <p>(I use Delphi XE on Windows 7.)</p>