digitalmars.D.learn - new int[]
- =?UTF-8?B?THXDrXM=?= Marques (14/14) Jan 10 2018 Due to compatibility with some C code, I basically need to do
- =?UTF-8?B?THXDrXM=?= Marques (3/5) Jan 10 2018 Just to be extra clear: I really do want a normal D slice, it
- Nathan S. (10/10) Jan 10 2018 Is there any problem with:
- ag0aep6g (11/27) Jan 10 2018 If I understand correctly, the goal is to have the `int[]` itself on the...
- Adam D. Ruppe (6/8) Jan 10 2018 General word of warning: if you pass it to C and the C function
- =?UTF-8?B?THXDrXM=?= Marques (4/9) Jan 10 2018 Thanks! I don't think it applies to me, because the void* pointer
- Nathan S. (12/14) Jan 10 2018 The code
- Jonathan M Davis (14/28) Jan 10 2018 If there are cases where it doesn't allocate, it probably depends on
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/37) Jan 10 2018 I was writing the same for a no-initial-value version:
- =?UTF-8?B?THXDrXM=?= Marques (5/14) Jan 10 2018 That's an interesting solution. I'm not sure which one I prefer,
- Dukc (6/15) Jan 11 2018 I believe this does allocate from the automanaged heap already.
Due to compatibility with some C code, I basically need to do this: struct Wrapper { int[] x; } void main() { void* ctxptr = new Wrapper([1, 2, 3]); auto context = cast(Wrapper*) ctxptr; writeln(context.x); } How can I do that without the wrapper? `new int[]` isn't supported, even though that's exactly what I want.
Jan 10 2018
On Wednesday, 10 January 2018 at 22:35:01 UTC, Luís Marques wrote:How can I do that without the wrapper? `new int[]` isn't supported, even though that's exactly what I want.Just to be extra clear: I really do want a normal D slice, it can't be a fixed-length array.
Jan 10 2018
Is there any problem with: ---- import std.stdio; void main(string[] args) { int[] x = [1, 2, 3]; writeln(x); } ---- https://run.dlang.io/is/CliWcz
Jan 10 2018
On 01/10/2018 11:35 PM, Luís Marques wrote:Due to compatibility with some C code, I basically need to do this: struct Wrapper { int[] x; } void main() { void* ctxptr = new Wrapper([1, 2, 3]); auto context = cast(Wrapper*) ctxptr; writeln(context.x); } How can I do that without the wrapper? `new int[]` isn't supported, even though that's exactly what I want.If I understand correctly, the goal is to have the `int[]` itself on the GC heap. You can make an `int[][]` with one element, and then take the address of that element: ---- void main() { int[]* x = &[[1, 2, 3]][0]; int[]* x2 = [[1, 2, 3]].ptr; /* same */ } ----
Jan 10 2018
On Wednesday, 10 January 2018 at 22:46:30 UTC, ag0aep6g wrote:If I understand correctly, the goal is to have the `int[]` itself on the GC heap.General word of warning: if you pass it to C and the C function holds on to that pointer for any reason beyond its immediate execution, you could be looking at a problem because the D GC can't see C function memory and may free it after the function returns.
Jan 10 2018
On Wednesday, 10 January 2018 at 22:48:48 UTC, Adam D. Ruppe wrote:General word of warning: if you pass it to C and the C function holds on to that pointer for any reason beyond its immediate execution, you could be looking at a problem because the D GC can't see C function memory and may free it after the function returns.Thanks! I don't think it applies to me, because the void* pointer is reachable from a root pointer on the D side.
Jan 10 2018
On Wednesday, 10 January 2018 at 22:46:30 UTC, ag0aep6g wrote:If I understand correctly, the goal is to have the `int[]` itself on the GC heap.The code ---- void main(string[] args) nogc { int[] x = [1, 2, 3]; } ---- won't compile, because "array literal in nogc function 'D main' may cause GC allocation". But "may" isn't the same as "will". What determines it? That's a kind of goofy error message now that I think about it.
Jan 10 2018
On Wednesday, January 10, 2018 22:50:22 Nathan S. via Digitalmars-d-learn wrote:On Wednesday, 10 January 2018 at 22:46:30 UTC, ag0aep6g wrote:If there are cases where it doesn't allocate, it probably depends on compiler optimizations. If it's able to determine that x doesn't escape the function, it might allocate it on the stack. I don't know. But nogc is about guaranteeing that it _won't_ allocate using the GC, so "may" is enough to make it illegal for nogc. It's also possible that this particular case will always allocate, but the error message is also used in other code where it's not guaranteed to allocate. Recently, in a discussion on improving error messages Walter did mention that there are cases in the compiler where the same piece of code generates errors for a variety of cases and that it would probably be better to make some of those less generic so that the error messages can be more specific. - Jonathan M DavisIf I understand correctly, the goal is to have the `int[]` itself on the GC heap.The code ---- void main(string[] args) nogc { int[] x = [1, 2, 3]; } ---- won't compile, because "array literal in nogc function 'D main' may cause GC allocation". But "may" isn't the same as "will". What determines it? That's a kind of goofy error message now that I think about it.
Jan 10 2018
On 01/10/2018 02:46 PM, ag0aep6g wrote:On 01/10/2018 11:35 PM, Luís Marques wrote:I was writing the same for a no-initial-value version: void main() { void *v = cast(void*)((new int[][](1)).ptr); *(cast(int[]*)v) ~= 42; } I hope it's correct. :o) AliDue to compatibility with some C code, I basically need to do this: struct Wrapper { int[] x; } void main() { void* ctxptr = new Wrapper([1, 2, 3]); auto context = cast(Wrapper*) ctxptr; writeln(context.x); } How can I do that without the wrapper? `new int[]` isn't supported, even though that's exactly what I want.If I understand correctly, the goal is to have the `int[]` itself on the GC heap. You can make an `int[][]` with one element, and then take the address of that element: ---- void main() { int[]* x = &[[1, 2, 3]][0]; int[]* x2 = [[1, 2, 3]].ptr; /* same */ } ----
Jan 10 2018
On Wednesday, 10 January 2018 at 22:46:30 UTC, ag0aep6g wrote:If I understand correctly, the goal is to have the `int[]` itself on the GC heap.That's correct.You can make an `int[][]` with one element, and then take the address of that element: void main() { int[]* x = &[[1, 2, 3]][0]; int[]* x2 = [[1, 2, 3]].ptr; /* same */ }That's an interesting solution. I'm not sure which one I prefer, the wrapper or this one. Still... I feel like the language should just allow allocating an array itself on the GC heap :(
Jan 10 2018
On Wednesday, 10 January 2018 at 23:08:28 UTC, Luís Marques wrote:I believe this does allocate from the automanaged heap already. At least it won't work with nogc. If one wants to reserve the memory from the program space, he needs to use module-level static arrays. With immutable stuff I don't know, perhaps it's implementation dependant.void main() { int[]* x = &[[1, 2, 3]][0]; int[]* x2 = [[1, 2, 3]].ptr; /* same */ }That's an interesting solution. I'm not sure which one I prefer, the wrapper or this one. Still... I feel like the language should just allow allocating an array itself on the GC heap :(
Jan 11 2018