Amazon.com Widgets Example 15.3 - Mutable or Immutable?
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2013, 01:40:10 AM
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 15 - Numbers, Strings, and Collections
| | | |-+  Example 15.3 - Mutable or Immutable?
Pages: [1]   Go Down
Print
Author Topic: Example 15.3 - Mutable or Immutable?  (Read 553 times)
melj247
Newbie
*
Posts: 5






« on: May 20, 2009, 12:14:11 PM »

In chapter 15 Example 3 the text states that 2 of the 3 NSString objects created at the beginning are immutable (cannot be changed).

Yet under // Copy on string to the end of another string
it appears that the value of str2 was CHANGED from "This is string B" to "This is string AThis is string B".

In the text it says that the result of [str1 stringByAppendingString: str2] creates a NEW string to hold the resulting value, but to me it appears that the resulting value is stored back into str2 (which is not supposed to change). I verified this at the end of the program by NSLog(@"%@", str2) which gives the output
"This is string AThis is string B."

I am soooooooooo confused by this. Can someone please clarify? What am I missing?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #1 on: May 20, 2009, 12:41:24 PM »

Remember that str2 is just a pointer to the string object in memory.  In the beginning of the program's execution, it's set pointing to the constant string object @"This is String B".  When this line of code gets executed:

Code: (Objective-C)
str2 = [str1 stringByAppendingString: str2];

the method does create a new string object and returns a pointer to it, which gets stored in the variable str2.   Refer back to pp. 172-174 and try to visualize str2 as a pointer to the string object in memory in an analagous manner to myPoint pointing to the XYPoint object in memory.

Cheers,

Steve Kochan
Logged
Mike R.
Newbie
*
Posts: 1






« Reply #2 on: June 02, 2009, 02:02:40 AM »

Doesn't this code create a memory leak? Back in program 7.6 we had a similar situation but used a temporary object to prevent a leak. Shouldn't we do the same here?

Thanks.

P.S. I'm enjoying your book.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #3 on: June 02, 2009, 03:03:51 AM »

If you're talking about the constant string object that str2 previously pointed to, you can't release those anyway.  If you're talking about any of the string (or other) objects returned by Foundation methods (like stringWithString:), you're not responsible for releasing their memory (they get autoreleased automatically--this is mentioned in the Chapter on Memory Management, but also in Chapter 15 i believe somewhere as well).  (As you'll learn, the exceptions are objects created using either alloc, new, or copy.)

Cheers,

Steve Kochan
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.