On Thursday, 11 June 2015 at 20:09:38 UTC, Adel Mamin wrote:
import std.stdio;

void main()
{
    ubyte[] a1 = new ubyte[65];
    ubyte[65] a2;

    writeln("a1.sizeof = ", a1.sizeof); // prints 16
    writeln("a2.sizeof = ", a2.sizeof); // prints 65
}

Why a1.sizeof is 16?

ubyte[] is a slice, which is actually a struct. It's more or less the same as:

struct Slice(T)
{
    T* ptr;
    size_t length;
}

So sizeof returns the size of the struct, not the size of the data that its ptr member points to.

ubyte[65] is a static array, which is just a big block of data on the stack. That's why it returns the expected value for sizeof. To create a slice of a static array, use the slice operator:

writeln(sizeof(a2[])); //Prints 16

Reply via email to