Amazon.com Widgets shallow copy
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 22, 2014, 10:30:32 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 18 - Copying Objects
| | | |-+ shallow copy
Pages: [1] Go Down
Print
Author Topic: shallow copy (Read 2599 times)
jnjpna
Newbie
*
Posts: 5


Email




on: March 07, 2011, 12:54:06 AM

I am confused as to the example presented in program 18.1 and 18.2.  As mentioned in program 18.2 the mutableCopy is a shallow copy which means the copied object will still have same references as the original object so both objects will point to the same memory location.  If following this understanding of shallow copy, I don't quite grasp going back to example 18.1 that removing a string object from dataArray2 didn't have any effect on dataArray since dataArray2 is a mutableCopy (shallow copy) thus both dataArray and dataArray2 should have the same references (just like program 18.2) to the same memory location...?  Appreciate if you can clarify this for me.  Thanks.
Logged
portreathbeach
Newbie
*
Posts: 4






Reply #1 on: March 07, 2011, 04:57:33 PM

I too am very confused. I thought I was understanding this until program 18.2...

Right, so in 18.1...
- dataArray2 is made equal to dataArray
- object at index 0 is removed from dataArray2
- both dataArray and dataArray2 have object at index0 removed

(this is all understood, as the pointers held in dataArray and dataArray2 are pointing at the same objects....

- dataArray2 is made equal to a mutableCopy of dataArray
- dataArray2 now has object at index0 removed
- dataArray stays as it was, dataArray2 has object 0 removed

(this is all as I would expect)

so now in 18.2...

- dataArray2 is made equal to a mutableCopy of dataArray
- mStr is made equal to the object stored at index0 of dataArray
(so I think I understand that mStr is pointing to the same piece of memory as index0 of dataArray)
- mStr has an appendString @"ONE" carried out
- dataArray object at index0 is now oneONE
- dataArray2 object at index0 is now oneONE Huh??

Why does dataArray2 get altered, when in 18.1 deleting an object from one array didn't affect the other?


Also, why does objective-C seem so complicated as opposed to C#, .NET and RealBasic. I have years of programming experience and never encountered these issues before using objects and arrays.
Last Edit: March 07, 2011, 04:59:18 PM by portreathbeach Logged
portreathbeach
Newbie
*
Posts: 4






Reply #2 on: March 07, 2011, 05:10:30 PM

I just re-read page 428 a few more times....

I think I may understand it a little bit more, but hopefully someone can confirm what I write.....

- dataArray2 has a mutable copy of dataArray, so that means that there are two arrays stored in memory
- these two arrays are store pointers to objects in memory (<<< the bit I'm not sure on)
- if you delete an object from an array the other in unaffected, as you have only deleted a pointer from an array
- if you change an object in an array, both arrays change as they point to the same memory space

Does this mean you could add to either array without affecting the other?


Thanks in advance for any help
Logged
jnjpna
Newbie
*
Posts: 5


Email




Reply #3 on: March 07, 2011, 07:36:26 PM

Thank you your reply!  I re-read many times and even read the documentation from Apple of shallow copy.  They all said copies from shallow copy is only another reference to the same memory location as the original copy.  Page 428, second paragraph, towards the end of that paragraph "...The net result was that the elements of both arrays referenced the same strings in memory.This is no different from assigning one object to another, which we covered at the beginning of this chapter."  The last sentence of this paragraph states it's no different from the simple assignment (dataArray2 = dataArray) the book shows at the beginning of this chapter.  If so, why even bother with shallow copy?  I think there must be a reason for shallow copy but it's just the results of both examples 18.1 and 18.2 throw me off from what I understand of reading shallow copy.

Your point of "pointer being removed" was interesting... but as I thought of it... if removed, the ultimate memory location, which both arrays are pointing should be affected, whether it be removed or changed... so... thanks for trying to help me.
Logged
portreathbeach
Newbie
*
Posts: 4






Reply #4 on: March 08, 2011, 01:05:41 AM

OK. The way I see it.....(I'm trying to refer each array as if it were a book)


dataArray is a pointer to a list of pointers which in turn point to the actual memory where the data is stored. So you could say dataArray is a pointer to the contents page of a book.

If you do dataArray = dataArray2, you make another pointer to the same contents page. So if you add or delete something in dataArray2, the contents page for each gets altered.

When you do the mutableCopy on dataArray, you create another 'contents' page, so you have dataArray pointing to one contents page, and dataArray2 pointing to another 'contents' page. These 'contents' pages still point to the same data, but now we can add and remove items from each array, because we are removing items in the 'contents' pages which are pointing to the actual data in memory.

If we append one of the objects in dataArray2, we are still ultimately changing the same piece of data which both 'contents' pages are pointing too.


Well, that's how I understand it  Undecided  I cannot understand why Objective-C has to be this complicated!
Logged
jnjpna
Newbie
*
Posts: 5


Email




Reply #5 on: March 08, 2011, 02:19:38 AM

Thanks for the illustration of "contents" page.  I kind of understand the differences between the simple assignment vs. shallow copy.  What I don't get is both simple assignment and shallow copy ultimately point to the same memory location so no matter if it's removed or changed, it should affect both arrays when trying to read that same data block whether from 1st or 2nd array...This is my first object oriented program so I guess I can't compare with other languages.
Logged
portreathbeach
Newbie
*
Posts: 4






Reply #6 on: March 08, 2011, 02:55:58 AM

This is what I understand...

With both simple assignment and shallow copy, the same bits of memory are being pointed too, but there are two 'contents' pages with shallow copy.

So, if you remove an object from dataArray2, the item in the 'contents' page is removed (the pointer to the memory where the object actually is), but not the actual object. I think this is because the actual object in memory has a retain count of 2 when both 'contents' pages are pointing to it, but when removed from one of the arrays, the retain count is decremented.

Obviously because both the arrays contents point to the same memory storage place, when changing a particular element in one, the other also changes.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #7 on: March 08, 2011, 09:24:55 AM

Yes, the explanations are accurate here.  With a simple assignment, you are just creating another reference to the same array.  So any operations through either variable are affecting that same array.  When you perform a copy, you are creating a new array with the same contents.  if it's a copy that's done, the new array's contents can't be changed.  If it's a mutableCopy, then the elements can be changed (e.g., the array can be sorted, and objects can be added to and removed from the copied array without affecting the original array).   

Cheers,

Steve
Last Edit: March 09, 2011, 09:03:41 AM by skochan Logged
jnjpna
Newbie
*
Posts: 5


Email




Reply #8 on: March 09, 2011, 07:19:59 AM

Thanks for staying so patient with me trying to explain it.  So I guess for shallow copy, the underlying operation of remove vs change is different... not symmetrical, meaning for removing object of mutableCopy, it just remove the duplicated pointer, but the actual data in the memory location is not removed.  But for changing the data, it DOES affect the actual data in that same memory location... is that correct?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #9 on: March 09, 2011, 08:59:03 AM

Yes, that's correct.

Cheers,

Steve
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.