in reply to Re: Passing a bytestring from Perl to Inline::C as a 2d array
in thread Passing a bytestring from Perl to Inline::C as a 2d array
pack "I*", (0) x ( $x_dimension x $y_dimension );
int **table = ( int ** ) byte_string;
table[ i ][ j ]++;
That still won't work and cannot be made to work! You will still get segfaults.
You cannot cast a 1D array point to a 2D array of arrays pointer. Full stop.
The memory for a 1D array (as produced by pack) is laid out so:
----------------------------....--------------------- ...|int1|int2|int3|int4|int4|....|N-3 |N-2 |N-1 |N | ----------------------------....---------------------
For a 2D array like so:
---- ptr | This is the int** ---- ... it points to an array of pointer below ... with one pointer for each of the first dimension --------------------------------------- ptr0|ptr1|ptr2|ptr3|....|Y-2 |Y-1 |Y | Each if these is an int * --------------------------------------- ... And each of those pointer point to contingous 1D arrays of ints ... each the size of the second dimension ... that can (and usually are) distributed at disparate position in me +mory ... and in no particular positions, even before the above block of ram +. --------....-----.....-----...etc |ary4| |ary2| |ary0| --------....-----.....-----...etc
There is simply no way to cast from one to the other.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Passing a bytestring from Perl to Inline::C as a 2d array
by Marshall (Canon) on Nov 14, 2009 at 15:35 UTC | |
by BrowserUk (Patriarch) on Nov 14, 2009 at 15:56 UTC | |
by Marshall (Canon) on Nov 14, 2009 at 16:47 UTC | |
by BrowserUk (Patriarch) on Nov 14, 2009 at 18:01 UTC | |
by Marshall (Canon) on Nov 14, 2009 at 19:14 UTC | |
|