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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)

In reply to Re^2: Passing a bytestring from Perl to Inline::C as a 2d array by BrowserUk
in thread Passing a bytestring from Perl to Inline::C as a 2d array by maasha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.