Unique ID for every objects using reinterpret_cast

Started by
14 comments, last by SiCrane 11 years, 5 months ago
I want my objects to have unique ids, but I heard someone saying using reinterpret_cast<ID>(*itself) will ensure that each objects has an unique ID since no two objects will ever have same memory addresses.
Is this a good idea?
An invisible text.
Advertisement
Probably not.

What happens if you save and reload the game? Every object will be in a different address. What happens when you free an object, and allocate another object later? It could potentially re-use the memory address even though the object is different.

Unique IDs per object can be great for many game designs. A simple number assigned to each object is generally sufficient.
no.

same memory address will be used after delete/free called on that object.
it is messy.

just use a id generator class. if you want something more advanced i recommend boost::Uuid
I think you need to think about what the unique ID is for.

If you ever need networking, then it's useless, because memory pointers will be different on different machines, if you save games, then you can't save/load your guids, because you can't guarantee the addresses will match when you load. You can't even guarantee that your guids won't be recycled (since the same address might be reallocated after an object is deleted.

So, given those problems, I can't see any advantage calling those things a Guid, rather than being explicit about it and calling it a pointer.

Basically, don't do it.
In 15 years of software development, I've never *actually* needed anything more than a sequential sequence starting at 1 for such a thing. I have thought I needed it, but in the end it turned out to be more trouble than it was worth.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Pointers are also different sizes depending on the target platform. I've worked on projects where the various servers and clients were a mixture of 64- and 32-bit applications, so it would have been very difficult to use any sort of platform-dependent ID type for objects that needed to be communicated between them.
If you don't want to worry about incrementing, then you can generate a UUID for every instance.
Just use an incrementing counter.

UUIDs are massive overkill for this kind of thing.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

if you need a very shortlived unqiueness, then the memory address will work fine.

Say you're sorting a handful of pointers, you can simply use the address.

One problem is if these are allocated non-continuous, each ptr is heap allocated, then the sort result will not be deterministic.
Your tools may sort your ai nodes in one order and the build process in another, so when you go to debug node 25 in your tool, it's not the same node 25 that the build process created.

A problem with incrementing IDs is that they wrap. For small tests it may appear that everything works fine. But after soaking your game for a couple days you will very likely wrap your IDs, then you need to deal with "well where do i get the next id from?" you'd need to returned freed ids to a list.

So if you need short lived, automatically recycling ids that simply guarantee uniqueness (locally) and are not deterministic, the memory address is perfectly applicable.
In most other cases, with multiple machines, or multiple runs, where the ids much be the same.. the memory address will fail miserably.
Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement