3

Let's say that I have declared an NSString like this

NSString *myString = [[NSString alloc] initWithString:@"Never Heard"];
NSString *tempString;

tempString = myString;

[myString release];

My question is why does it work? As you can see, I didn't alloc for the tempString. Therefore I don't think there is a need to release it. But if I try to alloc and init the tempString, it will bring an error.

NSString *myString = [[NSString alloc] initWithString:@"Never Heard"];
NSString *tempString = [[NSString alloc] init];

tempString = myString;

[myString release];

I use NSString as example, but instead I have different classes implemented. I'm trying to emphasize how memory allocation works here. Care to clarify and explain?

2
  • 1
    What do you mean by "But if I try to alloc and init the tempString, it will bring an error."? Could you please add the code that would cause the error?
    – vfn
    Mar 3, 2011 at 5:53
  • This is the problem inside your class.
    – Max
    Mar 3, 2011 at 6:04

3 Answers 3

3

A pointer is simply a memory address. You only create one object, and then you point tempString to that object. And tempString == myString.

[myString release] deallocates the string, leaving both pointers pointing at deallocated memory.

Do no confuse variables with objects. Variables are simply are handles that you use to access objects. Creating a new variable does not mean you are creating a new object.

10
  • I see. I really got confused with its for a while. Does that mean I can create as much pointer as I want without worrying about memory? Because I thought everytime I created a pointer, it will allocate small memory even if it just few bytes to point to the memory address.
    – sayzlim
    Mar 3, 2011 at 6:02
  • @Sayz Lim You confuse variables declaration and memory allocation.
    – Max
    Mar 3, 2011 at 6:06
  • It does take a small amount of memory, which is collected when the scope it was declared it exits (the method completing, for instance). But any object it points to may still exist, which is why you need to properly call retain/alloc/release methods on those objects. But usually your objects are much MUCH larger in memory than the pointers. It's not really a big concern making extra pointers.
    – Alex Wayne
    Mar 3, 2011 at 6:06
  • @Squeegy so whenever I declare variables, whether they are standard type or pointer. Their memory will be released once they out of their scope (like everytime method is executed and exit). I think I get it. Thanks for everyone helps.
    – sayzlim
    Mar 3, 2011 at 6:13
  • Local vars and pointer memory is freed, yes. But any objects the pointers reference stay around. This is a very very very important distinction.
    – Alex Wayne
    Mar 3, 2011 at 6:16
2

This is absolutely not about memory allocation. This is all about how pointers work. When you do this:

tempString = myString;

tempString points to the same object as myString. So calling any method on tempString is the same as calling them on myString.

1
tempString = myString;

In the above statement both myString, tempString are pointing to the location where "Never Hard" is stored. So, there is no error.

And I didn't understand when you meant - "But if I try to alloc and init the tempString, it will bring an error."

Edit 1

The second code snippet is an example of memory leak. tempString is allocated memory location. Let's work on with example -

myString -> MemoryLocation_1 that has "Never Hard"
tempString -> MemoryLocation_2 and the location it is pointing to isn't intialized with any value.

Now, with this statement -
tempString = myString;

Both myString  and tempString -> MemoryLocation_1 that has "Never Hard"

What about the MemoryLocation_2 obtained from the free store. It isn't returned back to free store and is lying there which no program can access to until the program termination. Thus giving memory leak. Hope it helps to an extent to understand.

5
  • That means I can actually create pointers as much as I want without worrying about memory? Actually when I try to do what you call as "Shallow Copy" on the second snippets. It can't even run, bringing up the NSInvalidArgumentException error.
    – sayzlim
    Mar 3, 2011 at 6:08
  • Exceptions are runtime errors. So, in this case you will not even get a run time exception but a memory leak. But if you try send message to a released object, it would result the exception. Just declaring a pointer doesn't mean that it is pointing to a valid memory allocation. This article should help you when memory is actually allocated. interfacelab.com/objective-c-memory-management-for-lazy-people
    – Mahesh
    Mar 3, 2011 at 6:16
  • Wut? There is no shallow copy or copy assignment operator here at all. tempString = myString; is an assignment of whatever pointer sized value happens to be in myString to tempString; it is nothing more than "shove these 4 bytes or 8 bytes from hither to yon". It is equivalent to saying x = 5; y = x;.
    – bbum
    Mar 3, 2011 at 7:31
  • @bbum - NSString isn't equivalent to int. NSString is a class and should have it's copy assignment operator function explicitly declared AFAIK.
    – Mahesh
    Mar 3, 2011 at 8:00
  • @bbum - Got you. In Obj-C every object is a pointer. There is no object residing on stack. Answered it with my C++ knowledge though. Will correct it. Thanks for pointing out.
    – Mahesh
    Mar 3, 2011 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.