从哪里获得新分配的数据结构成员的唯一ID [英] Where to get a unique id for a newly allocated datastructure member

查看:62
本文介绍了从哪里获得新分配的数据结构成员的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在使用OpenBSD编写一个网页游戏,但是我还想在Linux和Cygwin上保持运行状态。

我有一个表格列表,玩家/ kibitzer可以坐下来或者如果他想要的话,可以创建一个新的空表。


我使用漂亮的TAILQ_ *宏将表保存在双向链表中:
http://www.openbsd.org/cgi-bin/man.cgi?query=queue

后来我想切换到红黑哈希 - RB_ *宏:
http://www.openbsd.org/cgi-bin/man.cgi?query=tree

(适用于Linux和Cygwin我只是复制OpenBSD的tree.h和queue.h。


我的问题是获得一个新分配的

表的唯一ID结构体。以下是我考虑过的选项:


1)使用纪元秒 - 不好,竞争条件

2)使用随机数 - 不是很好,和上面一样

3)使用序号 - 我必须循环浏览

我的整个双向链表列表以验证它是唯一的

4)使用创建播放器的ID - 不能这样做,

因为我不想放弃所有其他玩家/ kibitzers
$如果第一个玩家想要站起来,b $ b离开桌子

(并且可能移到另一个桌子)

5)使用新创建的表结构的地址 -

似乎还可以,但需要很长的类型...


有人请其他一些想法吗?


谢谢

Alex


-
http://preferans.de

解决方案

A. Farber写道:


您好,


我在OpenBSD上编写网页游戏,但是我是

也试图在Linux和Cygwin上保持运行状态。


我有一个表格列表,其中一个玩家/ kibitzer可以

坐下或者如果他想要的话,创建一个新的空的。


我使用漂亮的TAILQ_ *宏将表保存在双向链表中:
http://www.openbsd.org/cgi-bin/man.cgi ?query = queue

以后我想切换到红黑哈希 - RB_ *宏在:
http://www.openbsd.org/cgi-bin/man.cgi?query=tree

(对于Linux和Cygwin,我只是复制OpenBSD的tree.h和queue.h)。


我的问题是得到新分配的

表结构的唯一ID。以下是我考虑过的选项:



我根本没有看到这是一个C问题。 (那你的代码

,大概是,C还不够。)难道没有comp.programming

更好吗?


-

克里斯微妙,像桶一样 Dollin

你是谁?你想要什么? / Babylon 5 /


Hello Chris,

Chris Dollin写道:


我根本没有看到这是一个C问题。 (你的代码

,大概是,C还不够。)难道没有comp.programming

更好?



我也在考虑其他新闻组:comp.unix.programming

(也许有一些操作系统技巧可以获得一个唯一的id), comp.algorithms

(但他们不会知道C宏,并建议我将我的编程语言切换到某些学术BS等等),等等。 ..


因为我不想交叉,我已经决定comp.lang.c将是b / b
最适合钓鱼的地方我正在寻找的技巧。

所以我不同意你的看法我的问题不合适:


我需要一个C技巧/宏/功能/通常模式的唯一身份。


问候

Alex


- -
http://preferans.de


一种。 Farber写道:


您好,


我在OpenBSD上编写网页游戏,但是我是

也试图在Linux和Cygwin上保持运行状态。


我有一个表格列表,其中一个玩家/ kibitzer可以

坐下或者如果他想要的话,创建一个新的空的。


我使用漂亮的TAILQ_ *宏将表保存在双向链表中:
http://www.openbsd.org/cgi-bin/man.cgi ?query = queue

以后我想切换到红黑哈希 - RB_ *宏在:
http://www.openbsd.org/cgi-bin/man.cgi?query=tree

(对于Linux和Cygwin,我只是复制OpenBSD的tree.h和queue.h)。


我的问题是得到新分配的

表结构的唯一ID。以下是我考虑过的选项:



....


3)使用序号 - 我必须循环浏览

我的整个双向链表列表以确认它是唯一的



为什么?

如果你有一个id,你每次需要时都会增加它 - 它是

唯一(至少包裹它)。


(如果它需要在多个

系统中是唯一的,则会出现问题。如果它在会话之间需要是唯一的,则存储

当前id并将其读回在下一次开始/会议时)


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

解决方案

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/


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


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)


这篇关于从哪里获得新分配的数据结构成员的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆