Array length & allocation question

Oskar Linde oskar.lindeREM at OVEgmail.com
Mon Jun 12 16:05:04 PDT 2006


Derek Parnell skrev:

> I sugest that Walter either document this functionality or fix it.

I agree that it should be better documented.

> 
> "When an array length is reduced the RAM it owns is not released and can 
> be reused when the array subsequently is expanded (, unless the length 
> is set to zero in which case the RAM is released). "
> 
> Setting the length to zero is a convenient way to reserved RAM for an 
> array.


t arr.length = 100_000_000; arr = arr[0..0]; is almost as convenient.

> Also consider this ...
> 
>     foo("");
> 
> Now how can 'foo' be written to detect a coder's error of passing it an 
> uninitialized array.
> 
>     char[] x;
>     foo(x);
> 

Like this:

void foo(char[] arr) {
	if (!arr)
		writefln("Uninitialized array passed");
	else if (arr.length == 0)
		writefln("Zero length array received");
}

/Oskar



More information about the Digitalmars-d-learn mailing list