views:

1463

answers:

5

What can be the best algorithm to generate a unique id in C++? The length ID should be a 32 bit unsigned integer.

+3  A: 

You can see this. (Complete answer, I think, is on Stack Overflow.)
Some note for unique id in C++ in Linux in this site. And you can use uuid in Linux, see this man page and sample for this.

If you use windows and need windows APIs, see this MSDN page.

This Wikipedia page is also useful: http://en.wikipedia.org/wiki/Universally_Unique_Identifier.

SjB
yes,would like to use linux. Thanks for the links.
Ajay
A: 

DWORD uid = ::GetTickCount(); ::Sleep(100);

an0nym0usc0ward
Sorry, in theory, if two machines do this on the same time - both machine will get the same uid, so the number is not unique across computers. See Neil's comment to the main question. It's correct if the demand is only to get uniqueness on the same machine across time, and not uniqueness across machines.
elcuco
Plus GetTickCount() is not very precise. You could call it three times in a row and get the same response.
John Dibling
+9  A: 

Getting a unique 32-bit ID is intuitively simple: the next one. Works 4 billion times. Unique for 136 years if you need one a second. The devil is in the detail: what was the previous one. You need a reliable way to persist the last used value and an atomic way to update it.

How hard that will be depends on the scope of the ID. If it is one thread in one process you only need a file. If it is multiple threads in one process you need a file and a mutex. If is multiple processes on one machine you need a file and a named mutex. If it is a multiple processes on multiple machines then you need to assign a authoritative ID provider. A database engine is a common provider like that, they have this built-in as a feature, an auto-increment column.

The expense of getting the ID goes progressively up as the scope widens. When it becomes impractical, scope is Internet or provider too slow or unavailable, then you need to give up on a 32-bit value. Switch to a random value. One that's random enough to make the likelihood that the machine is struck by a meteor is at least a million times more likely than repeating the same ID. A goo-ID. It is only 4 times as large.

Hans Passant
A: 

There is little context, but if you are looking for a unique ID for objects within your application you can always use a singleton approach similar to

class IDGenerator {
   public:
      static IDGenerator * instance ();
      uint32_t next () { return _id++; }
   private:
      IDGenerator () : _id(0) {}

      static IDGenerator * only_copy;
      uint32_t _id;
}

IDGenerator *
IDGenerator::instance () {
   if (!only_copy) {
      only_copy = new IDGenerator();
   }
   return only_copy;
}

And now you can get a unique ID at any time by doing:

IDGenerator::instance()->next ()

ezpz
static IDGenerator return Generator; }
RA
A: 

Here's the simplest ID I can think of.

MyObject obj;
uint32_t id = reinterpret_cast<uint32_t>(&obj);

At any given time, this ID will be unique across the application. No other object will be located at the same address. Of course, if you restart the application, the object may be assigned a new ID. And once the object's lifetime ends, another object may be assigned the same ID.

And objects in different memory spaces (say, on different computers) may be assigned identical IDs.

And last but not least, if the pointer size is larger than 32 bits, the mapping will not be unique.

But since we know nothing about what kind of ID you want, and how unique it should be, this seems as good an answer as any.

jalf
Bad idea. Imagine a 64 bits application. There is a very large chance that 32 bits of that address will be 0x00000000, or at least the same for all object addresses in a program. (Note: truncating reinterpret_cast results are likely to depend on endianness)
MSalters
I already pointed that out. If the pointer size is larger than 32 bits, you'll get collisions. But since we know virtually nothing about what the ID is needed for, or in which type of application, I just wanted to suggest the simple and obvious solution.
jalf