Hi all,

For the life of me, I can't figure out how I am segfaulting.  The following is a 
minimal test
case.  I think something's not getting initialized and thus causing some problem with 
memory, but
I can't tell what it is.

Output from the command line resembles:

  Index = 0.  String is 1.  Value = 1.000000
  Segmentation fault

If I run this from vi, I get a bit further, which is why I think that's it's a memory 
problem of
some sort.

  Index = 0.  String is 1.  Value = 1.000000
  Index = 1.  String is 2.  Value = 2.000000
  Index = 2.  String is 3.4.  Value = 3.400000

  Command terminated

Cheers,
Ovid

--

#!/usr/local/bin/perl
use strict;
use warnings;

use Inline C => <<'END_OF_C_CODE';
void train(SV* input)
{
    int i,length;
    AV *array;
    char* string;
    double value = 0;

    if (SvROK(input) && SvTYPE(SvRV(input)) == SVt_PVAV) {
        array  = (AV*)SvRV(input);
        length = av_len(array)+ 1;
        
        for (i = 0; i < length; i++) {
            SV *elem, **tmp = av_fetch(array, i, 0);
            
            if (!tmp)
                croak("Error fetching array index %i", i);
            elem = *tmp;
            string = SvPV(elem, PL_na);
            if (looks_like_number(string)) {
                value = atof(string);
                printf("Index = %d.  String is %s.  Value = %f\n", i, string, value);
            }
            else {
                printf("%s does not look like a number\n");
            }
        }
    }
    else 
        croak("train() takes an arrayref.");
}
END_OF_C_CODE

train( [1.0, 2.0, 3.4, 5.7] );


=====
Silence is Evil            http://users.easystreet.com/ovid/philosophy/indexdecency.htm
Ovid                       http://www.perlmonks.org/index.pl?node_id=17000
Web Programming with Perl  http://users.easystreet.com/ovid/cgi_course/

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

Reply via email to