Ok--- So, I guess this goes along with my confusion about arrays and pointers...

I have this code:

  @implementation Autocorrelator

  +(float *)coefficientsFor:(Buffer *)buffer {
      float coefficients[11];

      for (int i = 0; i <= 10; i++) {
          coefficients[i] = [self aForLag:i buffer:buffer];
      }
      return coefficients;
  }

  +(float)aForLag:(NSUInteger)lag buffer:(Buffer *)buffer {
      NSUInteger samples = [buffer size] - lag;
      float sum = 0.0f;

      for (int i = 0; i < samples; i++) {
          sum += buffer.samples[i] * buffer.samples[i + lag];
      }

      return sum;
  }

  ...

which I would like to call like this:

  -(void)something {
    float *coefficients = [Autocorrelator coefficientsFor:self.buffer];
    // do something with the coefficients array
  }

However, I am getting a compiler warning saying that coefficientsFor: is
returning a stack memory address associated with a local variable, which makes
sense...

but if I change

  float coefficients[11];

to:

  float *coefficients;

That warning goes away, but I don't understand why that would be acceptable..
How does it know the size of the array? Does it not matter?

The other way I can imagine doing this would be:

  +(void)coefficientsFor:(Buffer *)buffer coefficients:(float *)coefficients {
      for (int i = 0; i <= 10; i++) {
          coefficients[i] = [self aForLag:i buffer:buffer];
      }
  }

and have my caller do:

  -(void)something {
    float coefficients[11];
    [Autocorrelator coefficientsFor:buffer coefficients:coefficients];
    // do something with coefficients array;
  }

...

Am I on the right track?  Are these my only options?


Patrick J. Collins
http://collinatorstudios.com


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to