bbcannon [10/06/02 09:30 -0600]:
> I am receiving a "parse" error when I try to declare an array of doubles.
> However, I don't see a problem with my syntax.
> 
> Is something special required to create arrays with Inline ?

The short answer:

Your syntax is valid Perl, almost valid C++, but not valid C. To create
(static) arrays within C, you must know the size of the array at
compile time.

The long answer:

Let me paraphrase the important section of your code:

    void
    some_function(SV *x) {
        AV *arr;
        int maxIndex;
        int numElements;
        
        arr = SvRV(x);
        maxIndex = av_len(arr);
        numElements = maxIndex + 1;

        double data[numElements];
    }

That's not legal C for two reasons:

   1. You can only declare new variables at the beginning of a new scope,
      before the first non-declaration.

      /* This is legal: */
      {
         int hello;
         printf("foo!\n");
      }

      /* This is not */
      {
         printf("foo!\n");
         int hello;
      }

      Move the "double data" declaration up to the top of the scope, above
      the first non-declaration statement. That leads us to...

   2. You can only declare fixed-size static arrays if the size is known at
      compile-time. In your case, where the size depends on the length of the
      AV*, you will have to use New() or Newz() to allocate a temporary array
      yourself:

      {
         int maxIndex, numElements;
         AV *arr;
         double *data;

         ...
         
         Newz(0, data, numElements, double);
         SAVEFREEPV(data); /* Safefree() 'data' on scope exit */
         
         ...
      }

Notice the SAVEFREEPV(), which automatically calls Safefree() for you when
you leave the Perl XSUB, no matter how you leave it. Pretty neat, IMHO.

Hope that helps!

Later,
Neil

Reply via email to