On Tue, 4 Dec 2001, Scott Prahl wrote:
> I offer the following up for inclusion in C-Cookbook to save someone else the
> pain of doing this again --- particularly becasue there are not many examples
> in the Cookbook that deal with arrays and doubles.
Cool - you beat me to it. I was planning to dig up some dusty old XS and
give you a too-complicated example!
Some suggestions:
> angles = (double *) malloc(n*sizeof(double));
You should use New() here - the Perl API wrapper around malloc.
> av_ptr = (AV*) SvRV(array_ref);
You need to check for SvROK and that SvTYPE is what you want it to be.
Failing to do either can lead to seg faults on bad parameters.
> pcos = newAV();
> psin = newAV();
> for (i=0; i<n; i++) {
> av_push(pcos, newSVnv(cosine[i]));
> av_push(psin, newSVnv(sine[i] ));
> }
If this is performance sensitive code then you might want to pre-extend
your AVs to n to avoid the reallocing that pushing will cause.
> /* get rid of C arrays */
> free(angles);
> free(sine);
> free(cosine);
Safefree() here if you use New() as suggested.
-sam