www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Arrray sizeof

reply RenatoL <rexlen gmail.com> writes:
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"??
Dec 24 2011
next sibling parent "Mr. Anonymous" <mailnew4ster gmail.com> writes:
On 24.12.2011 18:46, RenatoL 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"??
8 is the size of the int[] type, which contains two pointers (or a pointer and a size). To get 20, you can use: arr1[0].sizeof * arr1.length
Dec 24 2011
prev sibling parent "Regan Heath" <regan netmail.co.nz> writes:
On Sat, 24 Dec 2011 16:46:18 -0000, RenatoL <rexlen 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/
Dec 28 2011