Thanks Neil,

I do know the size of the array before hand since I
create it in perl first.  
I am a rookie when it comes to C, so thanks a million.

In the Big Picture, I am just trying to pass a perl array
to C because I can then interface to Matlab's Math and Graphics libraries.  

Is there a more efficient/easy way to send an array
or array_ref to Inline C?

Thanks...Brady

-----Original Message-----
From: Neil Watkiss [mailto:[EMAIL PROTECTED]]
Sent: Monday, June 10, 2002 10:20 AM
To: bbcannon
Cc: '[EMAIL PROTECTED]'
Subject: Re: Instantiating arrays with Inline


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