digitalmars.D.learn - convert int[][] to int**
- Andrea Fontana (5/5) Feb 20 2014 I'm pretty sure I've just read about this, but search engines are
- bearophile (5/8) Feb 20 2014 One way to do it (untested):
- Andrea Fontana (2/10) Feb 20 2014 Ok, so it seems there's no "built-in" ways...
- bearophile (4/5) Feb 20 2014 Yeah, and this is a very good thing :-)
- Dicebot (6/20) Feb 20 2014 You can't do it without allocation because memory layout is
- Chris Williams (8/12) Feb 21 2014 You should only have to copy the top list, though.
- Chris Williams (3/15) Feb 21 2014 Addendum: Note that bearophile's code probably works out to the
I'm pretty sure I've just read about this, but search engines are not useful in this case. I have a C api that need a int** params that represent a int[][]. How can I convert from d to c to pass it? For simple arrays, array.ptr seems to work...
Feb 20 2014
Andrea Fontana:I have a C api that need a int** params that represent a int[][]. How can I convert from d to c to pass it? For simple arrays, array.ptr seems to work...One way to do it (untested): int** pp = myDArray.map!(a => a.ptr).array.ptr; Bye, bearophile
Feb 20 2014
On Thursday, 20 February 2014 at 16:47:43 UTC, bearophile wrote:Andrea Fontana:Ok, so it seems there's no "built-in" ways...I have a C api that need a int** params that represent a int[][]. How can I convert from d to c to pass it? For simple arrays, array.ptr seems to work...One way to do it (untested): int** pp = myDArray.map!(a => a.ptr).array.ptr; Bye, bearophile
Feb 20 2014
Andrea Fontana:Ok, so it seems there's no "built-in" ways...Yeah, and this is a very good thing :-) Bye, bearophile
Feb 20 2014
On Thursday, 20 February 2014 at 16:55:51 UTC, Andrea Fontana wrote:On Thursday, 20 February 2014 at 16:47:43 UTC, bearophile wrote:You can't do it without allocation because memory layout is different for int** and int[][] in D - are.ptr in latter points to slice struct (pointer+length) as opposed to raw pointer in former.Andrea Fontana:Ok, so it seems there's no "built-in" ways...I have a C api that need a int** params that represent a int[][]. How can I convert from d to c to pass it? For simple arrays, array.ptr seems to work...One way to do it (untested): int** pp = myDArray.map!(a => a.ptr).array.ptr; Bye, bearophile
Feb 20 2014
On Thursday, 20 February 2014 at 17:02:15 UTC, Dicebot wrote:You can't do it without allocation because memory layout is different for int** and int[][] in D - are.ptr in latter points to slice struct (pointer+length) as opposed to raw pointer in former.You should only have to copy the top list, though. int*[] temp = new int*[ arr.length ]; for (size_t i = 0; i < arr.length; i++) { temp[i] = arr[i].ptr; } int** output = temp.ptr; Untested.
Feb 21 2014
On Friday, 21 February 2014 at 19:13:13 UTC, Chris Williams wrote:On Thursday, 20 February 2014 at 17:02:15 UTC, Dicebot wrote:Addendum: Note that bearophile's code probably works out to the same thing.You can't do it without allocation because memory layout is different for int** and int[][] in D - are.ptr in latter points to slice struct (pointer+length) as opposed to raw pointer in former.You should only have to copy the top list, though. int*[] temp = new int*[ arr.length ]; for (size_t i = 0; i < arr.length; i++) { temp[i] = arr[i].ptr; } int** output = temp.ptr; Untested.
Feb 21 2014