32-bit GUID
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.
You can take the unsigned int or the shorts of out of the GUID struct that's passed to CoCreateGUID().
You could also just pick random integer to start with and just increment it for each addition to the container.
Hope that helps.
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.
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.
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
Picking a random number and incrementing afterward seems reasonable enough.
Thanks
Quote:Original post by impulsionaudioCorrect.
The whole 128-bit GUID is unique but not necessarily each member of the struct. Or am I mistaken.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement