6

I want to use the actual pointer address (not marked volatile) to an object to uniquely identify the object.

Is this a bad thing to do? In general does system memory management move the objects and hence it's address about or is the pointer stable?

Thanks

mfc
  • 3,018
  • 5
  • 31
  • 43
  • 3
    Within your meaning of the terms, a pointer is stable. In C, a reallocation can invalidate a pointer. Any time your code moves out of a scope, that can invalidate pointers. But if the pointers remain in scope, they're stable enough. – Jonathan Leffler Apr 07 '12 at 02:37

5 Answers5

10

Your pointer is guaranteed to remain stable for the life of the object to which it points, unless you do something to break it. The OS does indeed move things around in memory, but that's physical memory - the virtual memory space the OS presents to your process will keep things at the same addresses.

jimw
  • 2,548
  • 15
  • 12
  • 3
    A pointer remains valid for the lifetime of the object it points to, not (necessarily) for the lifetime of the program. – Keith Thompson Apr 07 '12 at 03:01
  • Very true, but the OP was asking about 'stability', whether the object can move to a different address. 'Something to break it' would include free()ing or otherwise destroying the object. Nonetheless, it's an important point which I'll add to my answer. Thanks for noticing. – jimw Apr 07 '12 at 03:14
  • 1
    You can have a pointer to a local object. At the end of the scope, the object doesn't move, it (logically) ceases to exist, and the pointer becomes invalid. https://gist.github.com/2324873 – Keith Thompson Apr 07 '12 at 03:41
4

In C, the address of an object is constant for its lifetime. Note that per the C standard, realloc does not "move" an object; it allocates a new object with the same contents (for the shorter of the old and new length) and, if successful, frees the old object.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
4

Pointers are stable, however, you should be using pointers correctly other wise you code will be unstable.

A pointer will be valid until the moment an object dies, once an object dies then you will have what is known as a Dangling Pointer.

There are many ways to avoid dangling pointers, some of them are very advanced topics. For example, you could use "Handles" but that is more sophisticated approach and would require a different memory management than what is default.

The language you are using also matters for the way a pointer can be invalidated. You have your question tagged as C, C++ and Objective-C. In C, your pointer can be invalidated by a malloc and realloc, where in C++ your pointer can be invalidated by delete.

I highly suggest reading more on pointers if you are first being introduced, such as this article.

I also suggest reading more into std::shared_ptr.

josephthomas
  • 3,256
  • 15
  • 20
3

A pointer to an object is valid until the object dies (stable).

Using pointers for identification has its uses, but may not be the best thing to do.

NSGod
  • 22,699
  • 3
  • 58
  • 66
Pubby
  • 51,882
  • 13
  • 139
  • 180
3

iOS memory management does not move the object around. If you have a pointer to an object it is valid for the life of the object.

In your question you refer to a "pointer address". However I assume you mean a pointer to the object. Otherwise the answer to this question becomes more complicated.

ThomasW
  • 16,981
  • 4
  • 79
  • 106