Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I have a question about pointers and making copies to new memory locations. I understand pointers but I am using them in a complex (for me) situation and the results are not what I was expecting.

Let say this. The code below is shorthand for simpler explanations.

Code:
NSArray *array1 = alloc/init;
NSArray *array2 = alloc/init
NSMutableDictinary *dict1 = alloc/init;
NSMutableArray *mainArray = alloc/init;

[dict1 setValue:array1 forKey:@"array1String];
[dict1 setValue:array2 forKey:@"array2String];

[mainArray addObject:[dict1 copy];

So in the code above I created a couple of Arrays and a Dictionary. I added both arrays to the dictionary with key/value. I then created an array to hold everything called mainArray. The last line I want to make a copy of the dict1 so it remains intact with the current values, and the arrays retain there values.

By creating a COPY of the dict1, I have created a new location in memory to store a copy of the dictionary. But will this also make copies of the array pointers inside of the dictionary?

My end result is the mainArray is tied to an NSPopUpButton. The user selects an item and it retrieves the data.
 
By creating a COPY of the dict1, I have created a new location in memory to store a copy of the dictionary. But will this also make copies of the array pointers inside of the dictionary?

If the pointers are copied then they point to the same underlaying objects, basically incrementing the reference count on these objects. You can always print the values of the pointers to see if they reference the same addresses if you want to find out I guess. The documentation on this says:

The exact meaning of “copy” can vary from class to class, but a copy must be a functionally independent object with values identical to the original at the time the copy was made.

If the pointers are copies and point to the same underlaying objects, then modifying the objects in one of the copies will change them in the other copy as well since they are just references to the same objects.
 
By creating a COPY of the dict1, I have created a new location in memory to store a copy of the dictionary. But will this also make copies of the array pointers inside of the dictionary?

See these:
https://developer.apple.com/library...ceptual/DevPedia-CocoaCore/ObjectCopying.html
https://developer.apple.com/library.../Conceptual/Collections/Articles/Copying.html

In a nutshell, there's a difference between a shallow copy and a deep copy. To preserve a copy of the array with all its original contents means a deep copy.
 
In a nutshell, there's a difference between a shallow copy and a deep copy. To preserve a copy of the array with all its original contents means a deep copy.

Yeah, but strictly speaking a shallow copy isn't really a copy at all, it's just a new reference.
 
Yeah, but strictly speaking a shallow copy isn't really a copy at all, it's just a new reference.

I used the terminology from the documents I linked. "Shallow copy" is also a term that's widely used in OOP (i.e. not limited to Objective-C):
http://en.wikipedia.org/wiki/Object_copy

Even outside of OOP, "shallow copy" means to copy a composite item (e.g. array or struct) simply by replicating the bit-patterns of its fields, e.g. a shallow copy of a C array or struct doesn't take into account that some (or all) members may hold pointers.
 
I used the terminology from the documents I linked. "Shallow copy" is also a term that's widely used in OOP (i.e. not limited to Objective-C):
http://en.wikipedia.org/wiki/Object_copy

I know, but so what? I merely pointed out that the meaning in this context is not really a copy. Do you not agree? If so please explain.

An array of pointers is merely references to the same underlying objects if they point to the same addresses. That's not to say it's not useful of course, but it's still not really a copy at all.

Code:
char *s = "some string";
char *shallow = s;
char *deep = strdup(s);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.