Unique ID for every objects using reinterpret_cast

Started by
14 comments, last by SiCrane 11 years, 5 months ago
Personally I strongly suggest to use an incremental index 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).

Previously "Krohm"

Advertisement

Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?


Maybe there are duplicate entries in the container, and you want to correlate them. Programming is a mysterious world, with many mysterious tasks.
+1 to incremental index.
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.

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.

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 ;)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


[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?


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.

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


Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?

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.

This topic is closed to new replies.

Advertisement