digitalmars.D.learn - clear array
- Damian (8/8) Oct 15 2012 I know this is a bit of a dumb question and I have searched and
- Andrej Mitrovic (3/4) Oct 15 2012 clear(arr).
- Adam D. Ruppe (14/16) Oct 15 2012 The direct D analog to memset is:
- Jonathan M Davis (5/10) Oct 15 2012 Use destroy, not clear. clear is now an alias for clear and should be go...
- Damian (6/19) Oct 15 2012 destroy and clear work nicely, except when using large arrays dmd
- bearophile (16/20) Oct 15 2012 Unfortunately currently DMD/druntime is not designed to handle
- H. S. Teoh (12/20) Oct 15 2012 What about:
- Damian (2/23) Oct 15 2012 This does not handle multi-dimensional arrays
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
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
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
On Tuesday, October 16, 2012 02:37:52 Andrej Mitrovic wrote:On 10/16/12, Damian <damianroyday gmail.com> wrote: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 DavisDoes D have a built-in way to clear arrays dynamic and static?clear(arr). arr.clear() should work too.
Oct 15 2012
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:destroy and clear work nicely, except when using large arrays dmd is taking quite a while to compile int[1000][1000] arr; destroy(arr);On 10/16/12, Damian <damianroyday gmail.com> wrote: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 DavisDoes D have a built-in way to clear arrays dynamic and static?clear(arr). arr.clear() should work too.
Oct 15 2012
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
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
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:This does not handle multi-dimensional arraysI 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
Oct 15 2012