How to get the memory size of a dynamic array?

delphi

Solution

Between elements of a dynamic array there is no padding, `Length(MyArray)*SizeOf(Element)` should be accurate.

Problem

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?

Original source

Related problems