Sign in to follow this  
lride

Unique ID for every objects using reinterpret_cast

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
If you don't want to worry about incrementing, then you can [url="http://stackoverflow.com/questions/3247861/example-of-uuid-generation-in-c"]generate a UUID [/url] for every instance. Edited by alnite

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Personally I strongly suggest to [b]use an incremental index[/b] as well.
Except I've often found it useful to create those indices independantly, for each pool of objects so the id is
[font=courier new,courier,monospace](pool, objectIndex)[/font] where an object can be accessed simply by [font=courier new,courier,monospace]pool[objectIndex][/font].
This still has the easiness of the incremental pointer and provides some useful insight on the type of the object (if the various pools are type-coherent) or their lifetime (if the pool is temporally coherent).

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1353293216' post='5002210']
Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?
[/quote]

Maybe there are duplicate entries in the container, and you want to correlate them. Programming is a mysterious world, with many mysterious tasks.

Share this post


Link to post
Share on other sites
[quote name='Matias Goldberg' timestamp='1353337859' post='5002352']
Not to mention using memory ptrs makes your application undeterministic. Each time you run the program, if your data is sorted by pointers, the order will always be different.[/quote]
Unless, of course, your allocations are guaranteed to be in-order and contiguous placement allocations...

The world of memory-friendly optimisations is a strange and wonderful place ;)

Share this post


Link to post
Share on other sites
[quote name='bzroom' timestamp='1353330072' post='5002332']
[quote name='ApochPiQ' timestamp='1353293216' post='5002210']
Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?
[/quote]

Maybe there are duplicate entries in the container, and you want to correlate them. Programming is a mysterious world, with many mysterious tasks.
[/quote]

I'm hard pressed to think of a situation that can't be solved by better means than comparing pointer addresses. That's just begging for undefined behavior.

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1353293216' post='5002210']
Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?
[/quote]
Occasionally, sorting a container of pointers by pointer value can be a performance boost, provided that you need to do batch processes on all the elements in the container, the container contains a sufficient number of pointers, the objects pointed to are sufficiently small enough that multiple objects can fit on a cache line (preferably L1), but the data set is large enough that an appreciable number of cache misses occur, the objects are allocated in a way with low fragmentation and inline overhead and the container is not modified (and hence resorted) frequently. Usually a complete reorganization of data storage is more effective in this kind of situation, but it makes for a low programmer effort speed boost if it works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this