in reply to Using $a and $b from XS

Totally unrelated to the problem you were having I'm sure but why are you creating one more position in memory than you need?

Newz( 1, topN, n+1, SV* ); for( i = 0; i < n+1; i++ ) topN[ i ] = newSViv( 0 );

Update: When I change both instances of n+1 to n, the script continues to provide valid results. I just may have to start concentrating more time on learning XS. This is getting fun.