Re: Prevent Garbage Collector

2014-01-05 Thread Jacob Carlborg
On 2014-01-05 01:17, 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: int[] iryy = new int[](50); // Will the array elements be initialized to 0? Yes, have a look at:

Prevent Garbage Collector

2014-01-04 Thread Jeroen Bollen
Is there a way to prevent the Garbage collector from running on one particular object? Something like: int* CreatePermanentInt() { int i = 5; return i; } And i wouldn't be collected after this.

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
On Saturday, 4 January 2014 at 17:15:12 UTC, Jeroen Bollen wrote: Is there a way to prevent the Garbage collector from running on one particular object? Something like: I would just malloc it. int* CreatePermanentInt() { int* i = malloc(int.sizeof); *i = 5; return i; } The malloced

Re: Prevent Garbage Collector

2014-01-04 Thread Jeroen Bollen
On Saturday, 4 January 2014 at 17:16:53 UTC, Adam D. Ruppe wrote: On Saturday, 4 January 2014 at 17:15:12 UTC, Jeroen Bollen wrote: Is there a way to prevent the Garbage collector from running on one particular object? Something like: I would just malloc it. int* CreatePermanentInt() {

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
And GC.addRange is in core.memory if you want that. http://dlang.org/phobos/core_memory.html#addRange

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
On Saturday, 4 January 2014 at 17:19:30 UTC, Jeroen Bollen wrote: Do I get malloc from the C library or does D also have a function for this? import core.stdc.stdlib; // malloc and free are in here it uses it from the C library but the standard c lib is easily accessible from D in the

Re: Prevent Garbage Collector

2014-01-04 Thread Jeroen Bollen
On Saturday, 4 January 2014 at 17:22:44 UTC, Adam D. Ruppe wrote: On Saturday, 4 January 2014 at 17:19:30 UTC, Jeroen Bollen wrote: Do I get malloc from the C library or does D also have a function for this? import core.stdc.stdlib; // malloc and free are in here it uses it from the C

Re: Prevent Garbage Collector

2014-01-04 Thread Jeroen Bollen
On Saturday, 4 January 2014 at 17:24:24 UTC, Adam D. Ruppe wrote: And GC.addRange is in core.memory if you want that. http://dlang.org/phobos/core_memory.html#addRange Would that work with structs too? Struct* i = malloc(Struct.sizeof); i = Struct(params);

Re: Prevent Garbage Collector

2014-01-04 Thread anonymous
On Saturday, 4 January 2014 at 17:15:12 UTC, Jeroen Bollen wrote: Is there a way to prevent the Garbage collector from running on one particular object? Something like: int* CreatePermanentInt() { int i = 5; return i; } And i wouldn't be collected after this. i isn't GC managed

Re: Prevent Garbage Collector

2014-01-04 Thread bearophile
Adam D. Ruppe: int* CreatePermanentInt() { int* i = malloc(int.sizeof); *i = 5; return i; } In D malloc needs a cast void - T*. In D a wrapper like this could help you avoid some mistakes making the code more DRY (untested): T* cMalloc(T)() nothrow { return

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
On Saturday, 4 January 2014 at 17:37:00 UTC, Jeroen Bollen wrote: Would that work with structs too? Struct* i = malloc(Struct.sizeof); i = Struct(params); You don't want to take the address of a temporary, not with structs nor int. But you could copy it with *i = Struct(params) and that

Re: Prevent Garbage Collector

2014-01-04 Thread 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; }

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
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 ( =

Re: Prevent Garbage Collector

2014-01-04 Thread Jeroen Bollen
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

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
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

Re: Prevent Garbage Collector

2014-01-04 Thread Jeroen Bollen
Also about the previous C style malloc, to free it, I just use the c style delete?

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
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.

Re: Prevent Garbage Collector

2014-01-04 Thread Jeroen Bollen
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

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
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.

Re: Prevent Garbage Collector

2014-01-04 Thread bearophile
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

Re: Prevent Garbage Collector

2014-01-04 Thread Adam D. Ruppe
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: