Is this a valid use of an Objective-C pointer?

This is an entry-level question in which I wonder if this is a valid use of a pointer in an expression, for example:

NSMutableDictionary *foo = [bar mutableCopy]; 

I am embarrassed when this is true, and when I will need to do the following:

 NSMutableDictionary *foo = [[NSMutableDictionary alloc] initWithCapacity:0]; foo = [bar mutableCopy]; // use foo [foo release]; 

Are they valid? When to use one above the other?

+6
objective-c iphone cocoa-touch cocoa
source share
5 answers

Having guessed why you might be confused, I would like to add an explanation of what the second code example actually does (and why it is not needed).

 NSMutableDictionary *foo = [[NSMutableDictionary alloc] initWithCapacity:0]; 

I think you are confused by this line, because this line is often called "initialization foo". This is a bit misleading. Currently, 2 different objects are being changed here: a new NSMutableDictionary is being created, and the variable "foo" is assigned its address.

The line actually creates a new NSMutableDictionary object on the heap (application heap). I will name this "Dictionary 1". So that this new Dictionary 1 object on the heap can be found, its memory address is stored in foo. "foo" is a role as an index, so we can find "Dictionary 1".

Although we often say: “foo is a dictionary”, this is because we are lazy - the statement is technically incorrect. That's right: "there is a dictionary on the heap, and foo stores its memory address so that we can find and use it."

When you run the line:

 foo = [bar mutableCopy]; 

you use the address in the “bar” to find another object (I will call it “Dictionary 2”) on the heap and make another object (“Dictionary 3”) on the heap with the same values. If you are counting, now it is 3 objects .

After "Dictionary 3" is done, its memory address is then stored in the variable "foo". This save in "foo" overwrites the existing memory address (the one that points to "Dictionary 1"). This means that we have no other pointers to "Dictionary 1" and, therefore, he can never find it again. That's why we say that Dictionary 1 has leaked.

I hope you see in this situation why Dictionary 1 is never needed (you only ever intended to use foo to access a copy of Dictionary 3).

+9
source share

You will not need to write code in the second example. [[NSMutableDictionary alloc] initWithCapacity:0] does nothing but leak memory.

In the second example, you create an NSMutableDictionary and assign it to foo . Then on the next line you assign a copy of another NSMutableDictionary to foo , which means that the currently specified dictionary foo now simply flushed somewhere on the heap, cannot be freed.

You will need to let go of foo anyway, as described in the Objective-C memory management rules .

+7
source share

The first is correct, the second is a memory leak, because you are allocating twice (the number of copies as one) and freeing it only once.

Remember to release foo after you are done, but

+3
source share

The assignment act overwrites the previous value held by this variable. For example:

 x = 3 x = 4 

The value held by x at the beginning of the second line is 3, but after the line of code is executed it is 4. Syntax:

 int x = 0; 

actually just shortens this:

 int x; x = 0; 

Variables that are pointers do not differ from each other. Thus, if the first reference of a variable is simply assigned to it, you do not need to initialize it first, because any value that you initialize will simply be discarded.

+1
source share

The first is the right way to do this. Both copy and alloc allocate the memory of your variable. Thus, using the second option will increase the counter of saving your variable to 2.

0
source share

All Articles