473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Where to get a unique id for a newly allocated datastructure member

Hello,

I'm programming a web game on OpenBSD, but am
also trying to keep in runnable on Linux and Cygwin.

I have a list of tables at which a player/kibitzer can
sit down or create a new empty one if (s)he wants.

I keep tables in a doubly-linked list using the nice TAILQ_* macros:
http://www.openbsd.org/cgi-bin/man.cgi?query=queue
and later I'd like to switch to red-black hashes - the RB_* macros in:
http://www.openbsd.org/cgi-bin/man.cgi?query=tree
(for Linux and Cygwin I just copy OpenBSD's tree.h and queue.h).

MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:

1) Using epoch seconds - is not nice, race condition
2) Using a random number - not nice, same as above
3) Using a sequential number - I'd have to cycle through
my whole doubly linked list of tables to verify it is unique
4) Using the id of the creating player - can't do that,
because I don't want to drop all other players/kibitzers
off the table if that 1st player wants to stand up
(and maybe move to another table)
5) Using the address of the newly created table struct -
seems to be ok, but requires a long long type...

Does anybody please have some other idea?

Thank you
Alex

--
http://preferans.de

Dec 1 '06 #1
15 2026
A. Farber wrote:
Hello,

I'm programming a web game on OpenBSD, but am
also trying to keep in runnable on Linux and Cygwin.

I have a list of tables at which a player/kibitzer can
sit down or create a new empty one if (s)he wants.

I keep tables in a doubly-linked list using the nice TAILQ_* macros:
http://www.openbsd.org/cgi-bin/man.cgi?query=queue
and later I'd like to switch to red-black hashes - the RB_* macros in:
http://www.openbsd.org/cgi-bin/man.cgi?query=tree
(for Linux and Cygwin I just copy OpenBSD's tree.h and queue.h).

MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:
I don't see that this is a C question at all. (That your code
is, presumably, C isn't enough.) Wouldn't comp.programming be
better?

--
Chris "subtle, like a barrel" Dollin
"Who are you? What do you want?" /Babylon 5/

Dec 1 '06 #2
Hello Chris,

Chris Dollin wrote:
I don't see that this is a C question at all. (That your code
is, presumably, C isn't enough.) Wouldn't comp.programming be
better?
I was considering other newsgroups too: comp.unix.programming
(maybe there is some OS trick to get a unique id), comp.algorithms
(but they wouldn't know C macros and would suggest me to switch
my prog. language to some academic BS and so one), etc...

As I didn't want to crosspost, I've decided that comp.lang.c would
be the best place to fish for the trick that I'm looking for.
So I don't agree with you that my question is not appropriate here:

I need a C trick/macro/function/usual pattern for a unique id.

Regards
Alex

--
http://preferans.de

Dec 1 '06 #3
A. Farber wrote:
Hello,

I'm programming a web game on OpenBSD, but am
also trying to keep in runnable on Linux and Cygwin.

I have a list of tables at which a player/kibitzer can
sit down or create a new empty one if (s)he wants.

I keep tables in a doubly-linked list using the nice TAILQ_* macros:
http://www.openbsd.org/cgi-bin/man.cgi?query=queue
and later I'd like to switch to red-black hashes - the RB_* macros in:
http://www.openbsd.org/cgi-bin/man.cgi?query=tree
(for Linux and Cygwin I just copy OpenBSD's tree.h and queue.h).

MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:
....
3) Using a sequential number - I'd have to cycle through
my whole doubly linked list of tables to verify it is unique
Why ?
If you have one id whcih you increment it each time you need one - it is
unique (atleast until it wraps around).

(problems arise if it needs to be unique among multiple
systems. If it needs to be unique between sessions, you store the
current id and read it back upon the next start/session)
Dec 1 '06 #4
A. Farber wrote:
Hello Chris,

Chris Dollin wrote:
>I don't see that this is a C question at all. (That your code
is, presumably, C isn't enough.) Wouldn't comp.programming be
better?

I was considering other newsgroups too: comp.unix.programming
(maybe there is some OS trick to get a unique id),
(If there were, it would be off-topic here.)
comp.algorithms (but they wouldn't know C macros
Why does that matter? (And why would you want to use /macros/ for
this?)
and would suggest me to switch my prog. language to some academic BS
Really? Well, if that happened, you could just ignore that bit and
take whatever algorithm thingy they offered.
and so one), etc...

As I didn't want to crosspost, I've decided that comp.lang.c would
be the best place to fish for the trick that I'm looking for.
So I don't agree with you that my question is not appropriate here:

I need a C trick/macro/function/usual pattern for a unique id.
comp.programming would be better. There's /nothing/ C-specific about
your question. Expressing the computation, once you know what it is,
/that/ could be a C question.

--
Chris "subtle, like a barrel" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Dec 1 '06 #5
A. Farber said:
MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:

1) Using epoch seconds - is not nice, race condition
Agreed - that's a bad idea.
2) Using a random number - not nice, same as above
Not agreed. Construct a random (or at least pseudorandom) bit string using
as a seed the information you already have from your client. XOR it with an
arbitrary random bit string using some other seed (such as your real-time
clock, if you have access to it), both strings having the same length.

If you use enough bits, you can reduce the probability of a collision to a
far lower value than the probability that your brain will suddenly melt and
pour out through your nostrils. At that point, if you do not see the point
in concerning yourself with the latter possibility, then there is no
rational reason to be concerned about the former.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #6
What about GUIDs?

see http://en.wikipedia.org/wiki/Globally_Unique_Identifier

Maybe your operating system provides a function to create such an id.
For Windows check CoCreateGuid with google...

Cheers

Henryk

Dec 1 '06 #7
Richard Heathfield wrote:
Construct a random (or at least pseudorandom) bit string using
as a seed the information you already have from your client. XOR it with an
arbitrary random bit string using some other seed (such as your real-time
clock, if you have access to it), both strings having the same length.
Thanks, but what is the XORing meant for?
If you use enough bits, you can reduce the probability of a collision to a
far lower value than the probability that your brain will suddenly melt and
pour out through your nostrils. At that point, if you do not see the point
in concerning yourself with the latter possibility, then there is no
rational reason to be concerned about the former.
Regards
Alex

--
http://preferans.de

Dec 1 '06 #8
Hi,

Chris Dollin wrote:
A. Farber wrote:
I need a C trick/macro/function/usual pattern for a unique id.

comp.programming would be better. There's /nothing/ C-specific about
your question. Expressing the computation, once you know what it is,
/that/ could be a C question.
no, I still disagree. Maybe you know the book "Code Reading:
The Open Source Perspective" http://www.spinellis.gr/codereading/

At first glance it is about algrithms. But in fact it is a book
about getting better C reading (and programming) skills by
learning a list of very common C patterns.

And same is here: I don't care about algorithms in other
languages. I was looking for another C lego block to add
to my C toolbox. That's why I've asked my question here

Regards
Alex

--
http://preferans.de

Dec 1 '06 #9
A. Farber said:
Richard Heathfield wrote:
>Construct a random (or at least pseudorandom) bit string using
as a seed the information you already have from your client. XOR it with
an arbitrary random bit string using some other seed (such as your
real-time clock, if you have access to it), both strings having the same
length.

Thanks, but what is the XORing meant for?
Because my first thought was to do an MD5 hash on the information supplied
to you by the client, e.g. "Joe Surfer" + "9 yrs" + "USA" - and then I
realised that this wasn't necessarily unique, and would need
disambiguating, and I solved that problem by salting the information with
random bits. But of course if you have random bits, you don't need the
original bits.

So it was just sloppy thinking, really. :-)

The important point is that you only need, say, 256 bits of entropy to
render the probability of a collision vanishingly low - many orders of
magnitude lower than the chance of you winning the lottery jackpot. So
don't discard the random option. But a naive "id = rand()" is indeed asking
for trouble.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #10
A. Farber wrote:
Chris Dollin wrote:
>A. Farber wrote:
I need a C trick/macro/function/usual pattern for a unique id.

comp.programming would be better. There's /nothing/ C-specific about
your question. Expressing the computation, once you know what it is,
/that/ could be a C question.

no, I still disagree. Maybe you know the book "Code Reading:
The Open Source Perspective" http://www.spinellis.gr/codereading/
I don't, I'm afraid.
At first glance it is about algrithms. But in fact it is a book
about getting better C reading (and programming) skills by
learning a list of very common C patterns.

And same is here: I don't care about algorithms in other
languages. I was looking for another C lego block to add
to my C toolbox. That's why I've asked my question here
Then you're asking two separate questions: what's a good way to
generate a unique id; what's a good way to express that in C.
(Followup: how to perturb the algorithm to make it fit C
better. Were that to arise, then there might be some interesting
topicality issues.) The former isn't a C question, but it is
the question you asked.

I'd be interested in hearing what specific-to-C properties you might
be expecting from a unique-id generator.

I don't know why Richard H. thinks it's topical, but after all,
different reasonable people can have different opinions ...

--
Chris "subtle, like a barrel" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Dec 1 '06 #11
Chris Dollin said:

<snip>
I'd be interested in hearing what specific-to-C properties you might
be expecting from a unique-id generator.

I don't know why Richard H. thinks it's topical, but after all,
different reasonable people can have different opinions ...
Actually, I have a confession to make... :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #12
2006-12-01 <75******************************@bt.com>,
Richard Heathfield wrote:
A. Farber said:
>MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:

1) Using epoch seconds - is not nice, race condition

Agreed - that's a bad idea.
>2) Using a random number - not nice, same as above

Not agreed. Construct a random (or at least pseudorandom) bit string using
as a seed the information you already have from your client. XOR it with an
arbitrary random bit string using some other seed (such as your real-time
clock, if you have access to it), both strings having the same length.

If you use enough bits, you can reduce the probability of a collision to a
far lower value than the probability that your brain will suddenly melt and
pour out through your nostrils.
Wouldn't it be better to use the timestamp _and_ a random number? After
all, the probability is much lower for a short period of time [namely,
the time period during which some computers somewhere will have their
clock set to that time, not to mention that the probable _number_ of
computers that have a time set to that timestamp or will at any point
in the future quickly vanishes to a very small number] and the
possibility is closed once the number of such computers reaches zero,
whereas if you only use randomness, or if you negate the determinicity
of the timestamp by XORing it with randomness, the possibility of
a collision remains open forever.

I also suspect it would take a VERY large number of bits to literally
reduce the probability to that of anyone's brain suddenly melting
(though, once there, reducing it further to that of any _particular_
person's brain doing so will only take 35 or so more bits).

Adding the MAC address, too, will substantially reduce the number of
computers you have to worry about generating the same value, though it
has the disadvantage of making the generated value traceable to your
computer.
At that point, if you do not see the point
in concerning yourself with the latter possibility, then there is no
rational reason to be concerned about the former.
Dec 1 '06 #13
A. Farber wrote:
Hello,

I'm programming a web game on OpenBSD, but am
also trying to keep in runnable on Linux and Cygwin.

I have a list of tables at which a player/kibitzer can
sit down or create a new empty one if (s)he wants.

I keep tables in a doubly-linked list using the nice TAILQ_* macros:
http://www.openbsd.org/cgi-bin/man.cgi?query=queue
and later I'd like to switch to red-black hashes - the RB_* macros in:
http://www.openbsd.org/cgi-bin/man.cgi?query=tree
(for Linux and Cygwin I just copy OpenBSD's tree.h and queue.h).

MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:

1) Using epoch seconds - is not nice, race condition
very bad.
2) Using a random number - not nice, same as above
This is probably the best solution. Use a group generator to generate
pseudo random numbers.
3) Using a sequential number - I'd have to cycle through
my whole doubly linked list of tables to verify it is unique
Why? 2) is better if you don't want a monotonic sequence, but otherwise
3) is ok.

In both case, unicity can be garantee by number size. If you use a
64bits number and if your C code generate a new number at 5Ghz, it will
still take about 117 years before you get a collision.
4) Using the id of the creating player - can't do that,
because I don't want to drop all other players/kibitzers
off the table if that 1st player wants to stand up
(and maybe move to another table)
5) Using the address of the newly created table struct -
seems to be ok, but requires a long long type...
This is also a good solution, specially if you want small id which can
be recycled when not used.

a+, ld.
Dec 1 '06 #14
"A. Farber" <Al**************@gmail.comwrites:
[...]
MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:

1) Using epoch seconds - is not nice, race condition
2) Using a random number - not nice, same as above
3) Using a sequential number - I'd have to cycle through
my whole doubly linked list of tables to verify it is unique
4) Using the id of the creating player - can't do that,
because I don't want to drop all other players/kibitzers
off the table if that 1st player wants to stand up
(and maybe move to another table)
5) Using the address of the newly created table struct -
seems to be ok, but requires a long long type...
[...]

Unique in what context? If it only needs to be unique within one
execution of your program, a single integer variable, incremented for
each table allocation, will work just fine. If it needs to be unique
across multiple sequential executions of the program, store the
counter in a file between runs (writing the counter each time it
changes will protect you from program crashes).

Straying slightly from topicality ...

If it needs to be unique across concurrent executions of the same
program, possibly on different machines, it becomes more complicated.
Either you need a central source of uniqueness (which could become a
bottleneck), or you combine a unique identifier for this execution of
the program with something that's unique within the execution of the
program, or you rely on random numbers (use a good generator; rand()
typically isn't) and settle for a vanishingly small probability of a
collision. Methods for doing this are likely to require
system-specific extensions.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 1 '06 #15

A. Farber wrote:

<snipped>
MY PROBLEM is to get a unique id for a newly allocated
table structure. Here are the options I've considered:
static size_t last_uid = 0;

size_t get_uid (void)
{
return ++last_uid;
}

Write the unique ID to a file before exiting and read it
back in when starting the program.

If you need more unique identifiers than is possible with
size_t, then consider using an array of size_t of the size
you require and returning the array as a uid.
e.g.
static size_t last_uid[2];

Incrementing that number is left as an exercise to the reader :-)

goose,

<snipped>

Dec 1 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
0
by: Robert Potthast | last post by:
Hello, I want to make my garbage collector more safe. To make it more safe I need to know if an object has been allocated on the stack or on the heap using the operator new. My garbage collector...
2
by: Jo Goos | last post by:
Hello, I would like to count the unique values of a specific element in an XPath statement. Let's say I have the next XML document ... <CLUB> <MEMBER> <NAME>Fred</NAME>...
29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
1
by: Bae,Hyun-jik | last post by:
Where are __value objects allocated? I guess __value object is same to basic __value objects such as int,char,float, but C# compiler demands using new keyword even to __value classes. Please...
2
by: V. Jenks | last post by:
I have a C# class that generates a string based on 2 integers. One integer is equal to int.MinValue and the other is equal to int.MaxValue. Each time my class generates a new string from those 2...
9
by: dave m | last post by:
I need to be able to retrieve a unique ID from a users PC. I needs to be something a user could not easily change, like the computer name. Could someone point me in the right direction to find ...
4
by: haitao.song | last post by:
Hi, As it is always stated that value type is allocated on stack, while reference types are on managed heap. How about the struct with string members? stuct A { string str; } String type is...
6
by: Alvin SIU | last post by:
Hi all, I have a table in Db2 v8 like this: Team Name Role ------ -------- --------------------- A Superman Leader A Batman Member A WonderWoman Member B ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.