digitalmars.D.learn - Syntax for heap allocated void initialized arrays
- simendsjo (4/4) Sep 21 2013 This is incorrect, but what is the correct syntax? The arrays
- bearophile (13/17) Sep 21 2013 The simplest way to allocate a void-initialized GC-managed
- simendsjo (2/20) Sep 21 2013 Thanks. uninitializedArray works well for my need.
- bearophile (6/7) Sep 21 2013 uninitializedArray is the wrong function to use in 99.9% of the
- simendsjo (3/10) Sep 21 2013 I'm setting every element in the array, and every field of the
- bearophile (7/9) Sep 21 2013 I think that's sufficiently safe. If the GC run before you have
- Joseph Rushton Wakeling (3/6) Sep 21 2013 Thanks for clarifying where un vs. minimally matters -- I'd been wonderi...
- Timothee Cour (7/16) Oct 15 2013 just to clarify:
- bearophile (5/10) Oct 16 2013 I think so, but I am not an expert on GC matters, I have not yet
This is incorrect, but what is the correct syntax? The arrays page only says it's "an advanced feature", but doesn't show the syntax. int[] a = new int[1](void);
Sep 21 2013
simendsjo:This is incorrect, but what is the correct syntax? The arrays page only says it's "an advanced feature", but doesn't show the syntax. int[] a = new int[1](void);The simplest way to allocate a void-initialized GC-managed dynamic array in D is probably to use one of the two functions designed for such purpose inside std.array. I suggest to use minimallyInitializedArray and to avoid uninitializedArray unless you have very special needs. minimallyInitializedArray is GC-safer. auto a = minimallyInitializedArray!(int[])(1); If you don't want a dynamic array, but a static one, then the simplest solution is to wrap it with a static struct and allocate it using a GC.malloc... Bye, bearophile
Sep 21 2013
On Saturday, 21 September 2013 at 10:40:07 UTC, bearophile wrote:simendsjo:Thanks. uninitializedArray works well for my need.This is incorrect, but what is the correct syntax? The arrays page only says it's "an advanced feature", but doesn't show the syntax. int[] a = new int[1](void);The simplest way to allocate a void-initialized GC-managed dynamic array in D is probably to use one of the two functions designed for such purpose inside std.array. I suggest to use minimallyInitializedArray and to avoid uninitializedArray unless you have very special needs. minimallyInitializedArray is GC-safer. auto a = minimallyInitializedArray!(int[])(1); If you don't want a dynamic array, but a static one, then the simplest solution is to wrap it with a static struct and allocate it using a GC.malloc... Bye, bearophile
Sep 21 2013
simendsjo:Thanks. uninitializedArray works well for my need.uninitializedArray is the wrong function to use in 99.9% of the times. std.array docs probably have not explained you well enough the risks of its usage. Bye, bearophile
Sep 21 2013
On Saturday, 21 September 2013 at 11:36:32 UTC, bearophile wrote:simendsjo:I'm setting every element in the array, and every field of the element, so I should be safe, right?Thanks. uninitializedArray works well for my need.uninitializedArray is the wrong function to use in 99.9% of the times. std.array docs probably have not explained you well enough the risks of its usage. Bye, bearophile
Sep 21 2013
simendsjo:I'm setting every element in the array, and every field of the element, so I should be safe, right?I think that's sufficiently safe. If the GC run before you have initialized those fields, and some of those fields are references/pointers, that could cause memory leaks until the next time the GC runs. Bye, bearophile
Sep 21 2013
On 21/09/13 16:23, bearophile wrote:I think that's sufficiently safe. If the GC run before you have initialized those fields, and some of those fields are references/pointers, that could cause memory leaks until the next time the GC runs.Thanks for clarifying where un vs. minimally matters -- I'd been wondering that myself after our discussion on D.announce.
Sep 21 2013
On Sat, Sep 21, 2013 at 7:23 AM, bearophile <bearophileHUGS lycos.com>wrote:simendsjo: I'm setting every element in the array, and every field of thejust to clarify: is the following true? int*[N] a=void; foreach(i;N) a[i]=fillValue(i);// some leaks may occur during foreach loop //at the next GC run, no more leaks due to that piece of codeelement, so I should be safe, right?I think that's sufficiently safe. If the GC run before you have initialized those fields, and some of those fields are references/pointers, that could cause memory leaks until the next time the GC runs.Bye, bearophile
Oct 15 2013
Timothee Cour:is the following true? int*[N] a=void; foreach(i;N) a[i]=fillValue(i);// some leaks may occur during foreach loop //at the next GC run, no more leaks due to that piece of codeI think so, but I am not an expert on GC matters, I have not yet written a similar GC. Bye, bearophile
Oct 16 2013