There is a dynamic array consisting of records:

type ListType = (I,R,S,C,B); ElementRecord = record case TypeList: ListType of I: (IntField:integer); R: (RealField:real); S: (StrField:shortstring); C: (CharField:char); B: (BoolField:boolean); end; ExtArr = Array of ElementRecord; var RecList: ExtArr; 

Then we perform such actions with it (the array itself is inside the class and the methods work with it, here I show only the result of their work in order to avoid unnecessary code):

 SetLength(RecList, 1); RecList[0].TypeList := C; RecList[0].CharField := 'A'; Writeln(Sizeof(ExtArr), ' ', Sizeof(RecList), ' ', Sizeof(RecList[0])); SetLength(RecList, 2); RecList[1].TypeList := S; RecList[1].StrField := 'test'; Writeln(Sizeof(ExtArr), ' ', Sizeof(RecList), ' ', Sizeof(RecList[1])); 

Console output:

4 4 264
4 4 264

That is, the size of the type is 4 bytes, the size of the array is also 4, and the size of the record - the element of the array is 264 bytes. And all this does not depend on the contents of the array elements and their number. The size of what, after all, shows the function sizeof and how to use it correctly?

  • 2
    most likely the second value is the size of the address of the beginning of the array, and not of the array itself - Komdosh
  • one
    The first and second numbers are pointer size, since dynamic arrays are reference types. The size of the ElementRecord is determined by the longest member of the union - the shortstring , which can contain 255 characters plus some service bytes. - Igor
  • I wonder where the extra 8 bytes come from. Judging by the documentation wiki.freepascal.org/Character_and_string_types#ShortString - 255 bytes per character + 1 byte for the length (similar to Delphi). And that's all. Encoding does not turn on - CP_ACP by default is used. I know about alignment :), but how much smoother is 256 bytes ... @Risto, and did not experiment with the {$ Ax} directive? lazarus-ccr.sourceforge.net/fpcdoc/prog/… - kami
  • one
    Understood. Zero byte - the beginning of the variable part ( SizeOf(ListType) = 1 ), does not seem to be involved. Immediately after it (without alignment) there is a byte of the length of the string and the string itself. Total 257 bytes + alignment 8 bytes = 264. It turns out that the "extra" bytes are already after the line. If you put ListType = Word , then the string will start with 2 bytes (I look at the memory dump). Of course, this will not affect the overall size. - kami

1 answer 1

The first and second numbers are pointer size, since dynamic arrays are reference types. The size of the ElementRecord is determined by the longest member of the union - the shortstring , which can contain 255 characters plus some service bytes.

Add-comment from @kami:

Zero byte - the beginning of the variable part (SizeOf (ListType) = 1), does not seem to be involved. Immediately after it (without alignment) there is a byte of the length of the string and the string itself. Total 257 bytes + alignment 8 bytes = 264. It turns out that the "extra" bytes are already after the line. If you put ListType = Word, then the string will start with 2 bytes (I look at the memory dump). Of course, this will not affect the overall size.