clear array

Adam D. Ruppe destructionator at gmail.com
Mon Oct 15 17:39:43 PDT 2012


On Tuesday, 16 October 2012 at 00:33:04 UTC, Damian wrote:
> In C++ I would just use memset, I don't know if I should be 
> using this for D?

The direct D analog to memset is:

int[] arr;
arr[] = 0; // or whatever


You can assign a value to a whole slice in a single go. arr[] 
means return a slice of the entire array. You can also do a 
range: arr[0 .. 4] = 0;, though, of course, you don't want to go 
out of bounds then.

I believe arr[] = 0 actually compiles as memset, but however it 
is implemented, it does the same thing.


Note that this sets the content, but leaves the length the same. 
If you want to get rid of it all, you can just assign null:

arr = null;
assert(arr.length == 0);


More information about the Digitalmars-d-learn mailing list