Arrray sizeof

Regan Heath regan at netmail.co.nz
Wed Dec 28 03:25:25 PST 2011


On Sat, 24 Dec 2011 16:46:18 -0000, RenatoL <rexlen at gmail.com> wrote:

> snippet:
>
>
> int[] arr1 = [1,2,3,4,5];
> int[5] arr2 = [1,2,3,4,5];
> writeln(arr1.sizeof);
> writeln(arr2.sizeof);
>
> Output:
> 8
> 20
>
> "0 is ok to me but why "8"??

It's a quirk of D that int[] is a reference type, so you get the size of  
the reference (as My Anonymous said, a length and pointer) whereas int[5]  
is a value type, so you get the size of the value.

It's the same as the following C..

int *arr1;
int arr2[5];

printf("%d\n", sizeof(arr1));
printf("%d\n", sizeof(arr2));

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list