Pointer question

Discussion in 'Mac Programming' started by larswik, Dec 19, 2014.

  1. larswik macrumors 68000

    Sep 8, 2006
    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.

    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.
  2. subsonix macrumors 68040

    Feb 2, 2008
    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:

    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.
  3. chown33 macrumors 604

    Aug 9, 2009
    See these:

    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.
  4. subsonix macrumors 68040

    Feb 2, 2008
    Yeah, but strictly speaking a shallow copy isn't really a copy at all, it's just a new reference.
  5. chown33 macrumors 604

    Aug 9, 2009
    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):

    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.
  6. subsonix macrumors 68040

    Feb 2, 2008
    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.

    char *s = "some string";
    char *shallow = s;
    char *deep = strdup(s);
  7. larswik thread starter macrumors 68000

    Sep 8, 2006

Share This Page