Thank you all for your input.

I know that this problem may be solved in Perl or PDL, but now I am interested in exploring how it can be solved with Inline::C. I need speed, and I need compact.

Next, I see no problem in casting a bytestring using pack from Perl as a way of allocating a block of memory to be used in C as long as the block size is sufficient. That way the block of memory will be under Perl's memory management and garbage collection.

As I understand, you can overrule the Perl type from C by type casting, so the pack bytestring does not have to be an array of pointers.

Now, what is the problem with The "built-in" C 2D array? I should say that is exactly what I want - a 2D table of numbers. However, I will try my own index calculations as suggested.

Here is the code mock-up again - this time in code tags :o).

sub perl_routine { $byte_string = pack "I*", (0) x ( $x_dimension x $y_dimension ); c_function( $byte_string, $x_dimension, $y_dimension ); } void c_function( char *byte_string, int x_dimension, y_dimension ) { int **table = ( int ** ) byte_string; int i, j; for ( i = 0; i < x_dimension, i++ ) { for ( j = 0; j < y_dimension, j++ ) { table[ i ][ j ]++; } } }

Martin


In reply to Re: Passing a bytestring from Perl to Inline::C as a 2d array by Anonymous Monk
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.