January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | Also a somewhat unrelated question, variables in D get initialized by default, do they also when you define them right after? Something like: int[] iryy = new int[](50); // Will the array elements be initialized to 0? foreach(int i; irry) { i = 20; } |
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Sunday, 5 January 2014 at 00:17:12 UTC, Jeroen Bollen wrote:
> Also a somewhat unrelated question, variables in D get initialized by default, do they also when you define them right after? Something like:
Maybe. Logically, it is always initialized unless you explicitly tell it not to ( = void on declarations, not sure about making new return uniniialized memory), but the optimizer might see that the initalization is useless and skip it.
|
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 5 January 2014 at 00:28:00 UTC, Adam D. Ruppe wrote:
> On Sunday, 5 January 2014 at 00:17:12 UTC, Jeroen Bollen wrote:
>> Also a somewhat unrelated question, variables in D get initialized by default, do they also when you define them right after? Something like:
>
> Maybe. Logically, it is always initialized unless you explicitly tell it not to ( = void on declarations, not sure about making new return uniniialized memory), but the optimizer might see that the initalization is useless and skip it.
The 'might' here is worrying, as the array will be huge and it's really costly to initialize it twice. Is there a way to tell it to not initialize it? Is it safe to use foreach on it if it isn't initialized?
|
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Sunday, 5 January 2014 at 01:10:29 UTC, Jeroen Bollen wrote: > Is there a way to tell it to not initialize it? I'm not sure of any except using the primitives. You can malloc GC memory from GC.malloc (works the same way as C malloc, except you don't have to free it yourself; the GC does it). GC.malloc doesn't initialize the memory. http://dlang.org/phobos/core_memory.html#malloc I don't think there's a way to tell new not to zero it out though... > Is it safe to use foreach on it if it isn't initialized? Yeah, the contents will be random, but you are setting it anyway so that doesn't matter. |
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Also about the previous C style malloc, to free it, I just use the c style delete? |
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Sunday, 5 January 2014 at 01:30:22 UTC, Jeroen Bollen wrote:
> Also about the previous C style malloc, to free it, I just use the c style delete?
free() from core.stdc.stdlib.
|
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 5 January 2014 at 01:23:58 UTC, Adam D. Ruppe wrote:
> On Sunday, 5 January 2014 at 01:10:29 UTC, Jeroen Bollen wrote:
>> Is there a way to tell it to not initialize it?
>
> I'm not sure of any except using the primitives. You can malloc GC memory from GC.malloc (works the same way as C malloc, except you don't have to free it yourself; the GC does it). GC.malloc doesn't initialize the memory.
>
> http://dlang.org/phobos/core_memory.html#malloc
>
>
> I don't think there's a way to tell new not to zero it out though...
>
>> Is it safe to use foreach on it if it isn't initialized?
>
> Yeah, the contents will be random, but you are setting it anyway so that doesn't matter.
As GC.malloc returns a pointer, how does it know when it should garbage collect the allocated space?
|
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Sunday, 5 January 2014 at 02:08:56 UTC, Jeroen Bollen wrote:
> As GC.malloc returns a pointer, how does it know when it should garbage collect the allocated space?
The gc keeps track of the size and scans memory for any pointer into the region. If there's none, it collects it.
|
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam D. Ruppe:
> but the optimizer might see that the initalization is useless and skip it.
Currently D compilers seem not good at that. In std.array there are two functions to allocate arrays that are not or not fully initialized to avoid a double initialization.
Bye,
bearophile
|
January 05, 2014 Re: Prevent Garbage Collector | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sunday, 5 January 2014 at 02:17:00 UTC, bearophile wrote: > Currently D compilers seem not good at that. In std.array there are two functions to allocate arrays that are not or not fully initialized to avoid a double initialization. Oh cool, I forgot to check std.array. Here's the link: http://dlang.org/phobos/std_array.html#uninitializedArray Using that would surely be better than doing it yourself with GC.malloc. |
Copyright © 1999-2021 by the D Language Foundation