The following routine works:
SV *make_aoa_c( int n_rows, int n_cols ) {
int i, j;
char *foo = "foo";
AV *table = newAV();
AV *row;
for ( i = 0; i < n_rows; ++i ) {
row = newAV();
for ( j = 0; j < n_cols; ++j ) {
av_push( row, newSVpv( foo, 0 ) );
}
av_push( table, newRV_noinc( row ) );
}
return newRV_noinc( table );
}
$ ./751041.pl 1
1: 78836 (233391 us)
2: 78872 (216509 us)
3: 78872 (206672 us)
4: 78872 (206775 us)
5: 78872 (206308 us)
6: 78872 (207777 us)
7: 78872 (206677 us)
8: 78872 (206739 us)
9: 78872 (206675 us)
10: 78872 (205979 us)
No memory leak, and if I dump $table (e.g. using Data::Dumper, with a size of 3 x 3 or so), it holds the expected data...
(I think you were just doing more mortalizing than necessary... The newRV_noinc makes sure that the arrays' reference counts stay at 1, so they'll get freed, when the respective outer structure is being freed.)
For comparison, the pure-Perl implementation (still slightly faster/smaller):
$ ./751041.pl 0
1: 78696 (213442 us)
2: 78704 (208485 us)
3: 78704 (175431 us)
4: 78704 (175438 us)
5: 78704 (175422 us)
6: 78704 (175486 us)
7: 78704 (175667 us)
8: 78704 (175647 us)
9: 78704 (175682 us)
10: 78704 (175687 us)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.