I don’t know how to delete pointer but not an object, for example:
I have some class:
@interface model : NSObject {
NSMutableArray *tab;
}
And when I do this:
model1 = [[model alloc]init];
NSMutableArray * tab2 = [model1 tab];
...
some operations
...
I want to delete only a pointer to my tab which is *tab2, but when I’m releasing tab2, tab is releasing too. In c++ when I’m clearing I do this:
int a =10;
*w = &a;
and when I’m deleting a pointer do
delete w;
and variable a is still in memory and that’s is ok. What should I do in obj-c to delete only a pointer?
In your situation with Objective-C, there’s no reason to delete the pointer. Just let it fall out of scope. You’re not allocating any new objets. You’re not making a copy of tab. You’re not retaining it. You’re just creating another pointer to the original tab object. If you like, you can set
tab2 = nilbut it doesn’t really matter either way.In your second C++ example, I’m not certain, but you’re probably falling into undefined behavior because of the fact that the code example you gave actually works on the compiler you tested! It is not valid C++ to delete a pointer not created with
new.