Does the memory address of an object stay intact over its lifetime?

I am not sure if the memory address of an object keeps beeing the same over its lifetime. Does it? Or does it change sometimes during the object's existence?

Answers


Yes, the address of any given object is constant in Objective-C. This is rather important since objects are always referred to by address. :-) (Garbage collectors which move things about and update all pointers to them exist, but garbage collection isn’t supported on the iPhone and the Mac Obj-C garbage collector is documented not to do that – see Garbage Collection Programming Guide: Architecture, under How the Garbage Collector Works.)


If you mean self, then, yes, it stays intact over the lifetime of the object.


Although I have not gone indepth in the matter my views are as under:

Memory addresses of an object may not be static.

For example in Java, objects don't have pointers but references, the JVM might move around objects as part of its memory management scheme and might change the reference value in accordance to the moved object. Also objects might be moved around as part of the Garbage collection procedure of the JVM.

Although I have not read of any official documentation on this, in case you come across the same you could post it here.

The same process might be taking place in .Net.


In the Cocoa Garbage Collection Programming Guide I'm not seeing quite the ironclad assurance that Ahruman makes above that an object address is guaranteed permanent:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcArchitecture.html

Closed vs. Open Systems section:

'[In an open garbage collection system, collectors] reallocate and copy blocks of memory and update each and every referring pointer to reflect the new address. [...] Cocoa's garbage collector strikes a balance between being “closed” and “open” by knowing exactly where pointers to scanned blocks are wherever it can, by easily tracking "external" references, and being "conservative" only where it must.'

And with the general "dynamic" nature of the Cocoa runtime, I'd want a really explicit discussion of the subject in Apple documentation even for non-garbage-collected program. I don't find any statements along the lines of "the memory address of an object is guaranteed not to change" in searching the whole of developer.apple.com -- try Google with:

site:developer.apple.com cocoa "object's memory address" OR "memory address of an object" guaranteed OR permanent

And then there's that scary subject of... multi-threading (ahhhh).


Need Your Help

How can I 'save' a custom ribbon?

excel excel-vba menu vba

I have created several macro's for a specific Excel 2007 workbook that we keep on a SharePoint site. I have also created a custom QAT with a button for each of the macro's that we run. When I save ...