• Advertisement
Sign in to follow this  

32-bit GUID

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to create a 32-bit GUID to use as a key for associative containers. I say GUID because I don't have any identifying data (e.g. name) to feed to a hash function to generate a hash value for the key. Windows CoCreateGUID function creates a 128-bit GUID which is not what I want. Any ideas? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
You can take the unsigned int or the shorts of out of the GUID struct that's passed to CoCreateGUID().


typedef struct _GUID {
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID,


You could also just pick random integer to start with and just increment it for each addition to the container.

Hope that helps.

Share this post


Link to post
Share on other sites
GUID's are 128 bit. 32 bits and they're just ID's. prh99's suggestion sounds suitable. One call to CoCreateGUID and a cast to DWORD array[4] you've got 4, 32 bit id's.

Share this post


Link to post
Share on other sites
The whole 128-bit GUID is unique but not necessarily each member of the struct. Or am I mistaken.

Picking a random number and incrementing afterward seems reasonable enough.
Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by impulsionaudio
The whole 128-bit GUID is unique but not necessarily each member of the struct. Or am I mistaken.
Correct.

Share this post


Link to post
Share on other sites
Well, GUIDs are not unique anyway, they're very likely unique in the foreseeable future.

But the real question is: do you actual need the properties of GUIDs? The quote "as a key for associative containers" suggests to me that something like crc32(namestring) is maybe "unique enough". You probably don't own nearly enough keys to cause a collision in crc32.

Or, if you just want to give a distinct number to any object you read in from a file, so you can refer to it later, then something like: int id() {static int i; return ++i; } might just do.

You will only want the "GU" part if you absolutely need to make sure that an object you create on your PC today can be distinguished both from one you generate the next millisecond or in a year, and from one you generate on another PC at the same time. Most applications really don't need this.

Share this post


Link to post
Share on other sites
It all depends on where the IDs come from. If they're all generated from the one process on the one machine, then it's not really GUIDs you want, but just UIDs, in which case a cookie generator will do just fine. If they can come from anywhere then only a real 128bit GUID will help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement