.sizeof dynamically allocated array

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 11 13:14:23 PDT 2015


On Thursday, 11 June 2015 at 20:09:38 UTC, Adel Mamin wrote:
> Why a1.sizeof is 16?

sizeof is tied to *type*, not a variable. (I kinda wish a1.sizeof 
was prohibited, forcing you to say typeof(a1).sizeof so it is 
clear but whatever).

A dynamic array's size is the length variable plus the pointer 
variable.

A static array's size is the content itself.


If you want the size of the content in bytes, best way is to do 
(cast(ubyte[]) a1[]).length or somethign like that - use the 
length property instead of sizeof.


More information about the Digitalmars-d-learn mailing list