> On Mar 3, 2015, at 12:47 AM, Patrick J. Collins 
> <[email protected]> wrote:
> 
> but if I change
>  float coefficients[11];
> to:
>  float *coefficients;
> That warning goes away, but I don't understand why that would be acceptable..

It’s not. ‘coefficients’ is now an uninitialized pointer, i.e. it contains 
garbage, and reading or writing it is very likely to crash. The compiler ought 
to be warning you about this; maybe your target settings don’t have the 
uninitialized-variable warning enabled?

A pointer just points to something; it isn’t responsible for allocating memory 
for that something. By declaring the array as “float coefficients[11]” you did 
tell the compiler to allocate space for 11 items on the stack, but of course 
you can’t return a pointer to that.

You could modify the method to initialize the pointer by malloc’ing a buffer. 
Then the caller of the method becomes responsible for freeing the buffer, which 
is annoying.

The other option, which I believe is the most frequently used in practice, is 
to make the buffer pointer an input parameter rather than a return value. So 
the caller is responsible for finding space for the array, and passes it to the 
-coefficientsFor: method, which fills in the array. It would look something like
        + (void) getCoefficients: (float*)outCoefficients forBuffer: 
(Buffer*)buffer;

—Jens
 _______________________________________________
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