www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - clear array

reply "Damian" <damianroyday gmail.com> writes:
I know this is a bit of a dumb question and I have searched and 
not
found anything concrete surprisingly :/

Does D have a built-in way to clear arrays dynamic and static?
I don't want to iterate over every element and set a default 
value.

In C++ I would just use memset, I don't know if I should be using
this for D?
Oct 15 2012
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 10/16/12, Damian <damianroyday gmail.com> wrote:
 Does D have a built-in way to clear arrays dynamic and static?

clear(arr). arr.clear() should work too.
Oct 15 2012
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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);
Oct 15 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Tuesday, October 16, 2012 02:37:52 Andrej Mitrovic wrote:
 On 10/16/12, Damian <damianroyday gmail.com> wrote:
 Does D have a built-in way to clear arrays dynamic and static?

clear(arr). arr.clear() should work too.

Use destroy, not clear. clear is now an alias for clear and should be going away eventually. There were too many problems with the ambiguity of the name clear, so it was renamed. - Jonathan M Davis
Oct 15 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Oct 16, 2012 at 02:33:03AM +0200, Damian wrote:
 I know this is a bit of a dumb question and I have searched and not
 found anything concrete surprisingly :/
 
 Does D have a built-in way to clear arrays dynamic and static?
 I don't want to iterate over every element and set a default value.
 
 In C++ I would just use memset, I don't know if I should be using
 this for D?

What about: import std.algorithm; void main() { int[] arr = [1,2,3,4]; fill(arr, 0); writeln(arr); // should print [0,0,0,0] } ? T -- My program has no bugs! Only undocumented features...
Oct 15 2012
prev sibling next sibling parent "Damian" <damianroyday gmail.com> writes:
On Tuesday, 16 October 2012 at 00:44:15 UTC, Jonathan M Davis 
wrote:
 On Tuesday, October 16, 2012 02:37:52 Andrej Mitrovic wrote:
 On 10/16/12, Damian <damianroyday gmail.com> wrote:
 Does D have a built-in way to clear arrays dynamic and 
 static?

clear(arr). arr.clear() should work too.

Use destroy, not clear. clear is now an alias for clear and should be going away eventually. There were too many problems with the ambiguity of the name clear, so it was renamed. - Jonathan M Davis

destroy and clear work nicely, except when using large arrays dmd is taking quite a while to compile int[1000][1000] arr; destroy(arr);
Oct 15 2012
prev sibling next sibling parent "Damian" <damianroyday gmail.com> writes:
On Tuesday, 16 October 2012 at 00:47:44 UTC, H. S. Teoh wrote:
 On Tue, Oct 16, 2012 at 02:33:03AM +0200, Damian wrote:
 I know this is a bit of a dumb question and I have searched 
 and not
 found anything concrete surprisingly :/
 
 Does D have a built-in way to clear arrays dynamic and static?
 I don't want to iterate over every element and set a default 
 value.
 
 In C++ I would just use memset, I don't know if I should be 
 using
 this for D?

What about: import std.algorithm; void main() { int[] arr = [1,2,3,4]; fill(arr, 0); writeln(arr); // should print [0,0,0,0] } ? T

This does not handle multi-dimensional arrays
Oct 15 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Damian:

 destroy and clear work nicely, except when using large arrays 
 dmd is taking quite a while to compile

 int[1000][1000] arr;
 destroy(arr);

Unfortunately currently DMD/druntime is not designed to handle large fixed-sized arrays well, in general (large static floating point or char 2D arrays are the worst case). In such cases you sometimes have to use a dynamic array, or sometimes a little more efficiently a fixed sized array of dynamic arrays. In Bugzilla there are two ore more bug entries that ask for speeder compilations or better handling of such large fixed-sized arrays. My dmd isn't even running the program that "int[1000][1000] arr;" array inside the main, I think I have to increase stack size. Maybe you have found another case where dmd or its druntime are not well optimized, I have filed it in Bugzilla for you: http://d.puremagic.com/issues/show_bug.cgi?id=8828 See there for more info. Bye, bearophile
Oct 15 2012