How to declare a C array of integers as a property on an objective-c class?
5
-
1stackoverflow.com/questions/912016/…CoolMonster– CoolMonster2014-02-20 12:51:56 +00:00Commented Feb 20, 2014 at 12:51
-
1is this you want? stackoverflow.com/a/912344/2629258sathiamoorthy– sathiamoorthy2014-02-20 12:52:12 +00:00Commented Feb 20, 2014 at 12:52
-
YES thanks couldn't find itNicolas Manzini– Nicolas Manzini2014-02-20 12:54:03 +00:00Commented Feb 20, 2014 at 12:54
-
stackoverflow.com/questions/476843/…Rushabh– Rushabh2014-02-20 13:01:58 +00:00Commented Feb 20, 2014 at 13:01
-
Please see my updated answer. stack based arrays will probably get cleaned after the method call. Happy coding :)Basheer_CAD– Basheer_CAD2014-02-20 13:12:11 +00:00Commented Feb 20, 2014 at 13:12
Add a comment
|
1 Answer
@property (nonatomic, assign) int *array;
...
// somewhere in your code
int *gg = malloc(10 * sizeof(int));
gg[1] = 1;
gg[0] = 2;
self.array = gg;
UPDATE:
This is heap based array now to make sure it will not be deallocated.
But don't forget to free it in deallocfree(self.array)
5 Comments
Nicolas Manzini
exactly what i was trying to achieve. the cleanest answer from all in other similar questions
Basheer_CAD
great :)@NicolasManzini
creker
You can't do this. When you leave the scope where
gg was declared your array property will contain pointer to a deallocated part of memory. Best case scenario you will get trash instead of values you want. In case of int *array array must be allocated on heap using malloc or new.Nicolas Manzini
the funny thing is I was reading about this heap/stack problem right now! :) i can fill it with 0s by doing gg[] = {0} ?
Basheer_CAD
yep :) thanks to @creker for reminding :)