In Delphi, you can get the size of a value type with the sizeof()
compiler magic function, but calling sizeof()
on a reference type will give you the size of a pointer, not of the value it's pointing to.
For objects, you can get the memory size with the InstanceSize
method, but what about for dynamic arrays? Due to padding, length(MyArray) * sizeof(element)
may not be accurate. So, is there any accurate way to get the memory size of a dynamic array?
PChar(@MyArray[last+1]) - PChar(@MyArray[0])
? Of course,last + 1
might cause an index out of bounds exception, so you might want to uselast
instead and calculate the real size (padding and all) of each element and add that to it. – Lasse V. KarlsenLength(MyArray) * SizeOf(Element)
. – Ondrej KelleLength(MyArray) * SizeOf(ElementType)
will tell you how many bytes were allocated to hold the array's element data, but it will not tell you how many bytes were allocated for the entire dynamic array as a whole. – Remy Lebeau