www.digitalmars.com         C & C++   DMDScript  

D - Dynamic Arrays

reply elud verat.net writes:
Can someone explain me how dynamic arrays work in D?
How are they implemented in memory?
is it like this:
struct array
{
type *data;
uint size;
}
or like this
struct array // if size is 8 size of allocated memory is 4+8*4
{
uint size;
uint item[0];
}
Is int[] a reference or struct.

How then this code works?
int[] a; // if a is reference then a is null
a.length = 2; // this calls metod for null reference.
Oct 27 2003
parent "Walter" <walter digitalmars.com> writes:
<elud verat.net> wrote in message news:bnjhs6$2e6q$1 digitaldaemon.com...
 Can someone explain me how dynamic arrays work in D?
 How are they implemented in memory?
 is it like this:
 struct array
 {
 type *data;
 uint size;
 }
It's struct array { int length; type *data; }
 How then this code works?
 int[] a; // if a is reference then a is null
 a.length = 2; // this calls metod for null reference.
Assigning to a.length calls a magic library function. I suggest you try writing snippets of code you're curious about, and run obj2asm on it. I think that will make things a lot clearer.
Oct 27 2003