Sizeof in D

import std.stdio;

void main() {
    int[] a = [1,2,3,4,5,6,7,8,9,10];
    write(a.sizeof);
}

In the following code, the sizeof of a static array is 8 bytes. I am using x86 Windows 8, so the pointer is 4 bytes. Why am I getting an 8-byte array size?

+4
source share
2 answers

Because it int[]is a dynamic array, not a pointer. Arrays in D are not pointers. What they are is essentially

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

, , ptr, C/++ ( C/++ ). , length D , C/++. D, . , D.

, sizeof ptr length , 8 32- 16 64- .

+9

(, ) - , size_t, 4 .

D , ( ) O(1).

+7

Source: https://habr.com/ru/post/1546584/


All Articles