www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Syntax for heap allocated void initialized arrays

reply "simendsjo" <simendsjo gmail.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply "simendsjo" <simendsjo gmail.com> writes:
On Saturday, 21 September 2013 at 10:40:07 UTC, bearophile wrote:
 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
Thanks. uninitializedArray works well for my need.
Sep 21 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply "simendsjo" <simendsjo gmail.com> writes:
On Saturday, 21 September 2013 at 11:36:32 UTC, bearophile wrote:
 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
I'm setting every element in the array, and every field of the element, so I should be safe, right?
Sep 21 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
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
prev sibling parent reply Timothee Cour <thelastmammoth gmail.com> writes:
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 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.
just 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 code
 Bye,
 bearophile
Oct 15 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
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 code
I think so, but I am not an expert on GC matters, I have not yet written a similar GC. Bye, bearophile
Oct 16 2013