Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
September 21, 2013 Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
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); |
September 21, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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 |
September 21, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
September 21, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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
|
September 21, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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?
|
September 21, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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
|
September 21, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
October 16, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments:
| 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 > > |
October 16, 2013 Re: Syntax for heap allocated void initialized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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
|
Copyright © 1999-2021 by the D Language Foundation