Re: Accessor method for array of floats
Re: Accessor method for array of floats
- Subject: Re: Accessor method for array of floats
- From: Shawn Erickson <email@hidden>
- Date: Mon, 21 Jul 2003 18:32:08 -0700
On Monday, July 21, 2003, at 03:15 PM, Johan Kool wrote:
>
I am very grateful to the help I got so far, but I am still a little
>
stuck on my problem. I get this error "JKDataModel.m:63: error:
>
incompatible types in assignment" for the line where I say "time =
>
inArray;". I've tried every combination with asterisks and ampersands
>
I could think of, but I must say I don't completely get what the
>
problem is.
>
>
What am I doing wrong?
Why are you doing this " float *time[1] "? This is saying you have a
pointer to a float array with one entry. Instead you want a point to an
arbitrary length array that contain float sized entries. For that you
can simply use float* time.
Also in your getData method you are calling realloc on x which is
allocated on the stack not from the heap.
Finally you don't appear to be using the value return by realloc or at
least your code snippet doesn't show it. Realloc gives you back a
pointer, potentially a different point then the one you passed in.
>
float *localArray;
>
localArray = malloc([myModel numberOfPoints]*sizeof(float))
>
localArray = [myModel arrayOfFloats];
It this really how you are getting your array back out of your model?
If so the malloc call is allocating memory and sticking a pointer to it
into localArray. Then you ask your model for an array and that pointer
gets put into localArray. This results in the memory you allocated with
malloc to become unreferenced and hence lost resulting in a leaked.
So with modifications (typed in mail and not tested)...
@class JKMainDocument;
@interface JKDataModel : NSObject {
@private
int count;
float* time;
// -------------------
JKDataModel.h contains:
// -------------------
-(int)count {
return count;
}
-(void)setTime:(float *)inArray withCount:(int)inValue {
count = inValue;
time = (float*) realloc(time, count*sizeof(float));
memcpy(time, inArray, count*sizeof(float));
//note because your time method returns a pointer to your model's
time array
//you could get your own array back in this method, hence the above
realloc and copy
//wouldn't be needed in that particular case, an optimization could
be made by
//checking if the two pointer are the same.
}
-(float *)time {
//the following returns your models array directly, you may want to
return a copy if you
//don't want others to be able to modify it. If you return a copy
then someone has to free the
//memory, in cocoa that is usually the allocater of the memory but
in this case you can not
//leverage autorelease, hence the caller of the time method would
need to free the copy
//it gets. Since you are not returning a copy that is not an issue
but your models array can
//be changed or even realloc-ed by mistake out of existence...
return time;
}
// -------------------
JKMainWindowController.m contains:
// -------------------
- (void)getData
{
float* x;
int num_pts;
// some code must set num_pts
x = (float*) malloc(num_pts*sizeof(float));
// code assigning values to x[]
[[[self document] dataModel] setTime:x withCount:num_pts];
// -------------------
JKSomeOtherModel.m contains:
// -------------------
-(void)someFunction {
float *x;
int maxScan;
maxScan = [[[self document] dataModel] count];
// The following realloc currently result in a memory leak, it is
not needed because
// your time method return a pointer to your models array. Also
realloc is the wrong
// thing to use because x was never allocated in the first place in
this code snippet.
// realloc(&x, maxScan*sizeof(float));
x = [[[self document] dataModel] time];
// -------------------
In the end I personally don't see the point of your JKDataModel
object... it doesn't appear to be adding any value as currently
designed (not knowing what else it may do). The model is not handling
any of the data memory management for its callers.
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.