Re:C中的编程技术问题 [英] Re: A question of programming technique in C

查看:62
本文介绍了Re:C中的编程技术问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2008年11月4日星期二16:43:49 -0500,Eric Sosman

< Er ********* @sun.comwrote:

On Tue, 04 Nov 2008 16:43:49 -0500, Eric Sosman
<Er*********@sun.comwrote:


> Richard Harter写道:
>Richard Harter wrote:

>假设我们正在用C编写一个程序并且我们有一个
特定类型的对象(在OO编程意义上),我将调用bobbles。作为训练有素的程序员(哈!)我们创建了一个模块来将我们的bobble相关函数打包在一起。

如果我们使用基于面向对象的语言,那么将会有机器在
位置,构造函数,析构函数等,以处理我们模块的详细信息,但在C中我们必须自己动手。

我们需要做的一件事就是创建将作为构造函数和析构函数的函数。我们需要注意两件事,指定界面,并编写函数内部。我关心的是指定
界面。

我要投入一个曲线球。我们的程序将包含对创建的bobbles的引用;我们不知道它们在哪里如此
当我们删除bobbles时它们会变得陈旧。代码需要能够检测到引用已经过时并且适当地处理它。
>Suppose we are writing a program in C and that we have a
particular kind of object (in the sense of OO programming) that I
will call bobbles. Being well disciplined programmers (Ha!) we
create a module to package our bobble related functions together.

If we were using an OO based language there would be machinery in
place, constructors, destructors, and such like, to handle the
details of our module, but in C we have to roll our own.

One of the things we need to do is to create functions that will
serve as constructors and destructors. There are two things that
we have to take care of, specifying the interface, and writing
the function internals. My concern is with specifying the
interface.

I am going to throw in one curve ball. Our program will contain
references to created bobbles; we won''t know where these are so
they can go stale when we delete bobbles. The code will need to
be able to detect that a reference has gone stale and deal with
it appropriately.



据我所知,曲线与讨论的其余部分正交,所以我会忽略它。您的问题似乎涉及
表示隐藏和有效性检查,而不是垃圾收集。


As far as I can see, the curve is orthogonal to the rest of
the discussion, so I''ll ignore it. Your issues appear to deal with
representation hiding and validity checking, not with garbage
collection.



我可以稍后解决你的其他评论(为此,非常感谢你好b $ b)但这似乎是一个严肃的观点混乱

所以让我扩展。我们可以拥有的是以下序列的

事件:


在时间A,我们创建bobble X. Space将被分配给X作为
需要
,包括一个struct bobble_s。


在时间B我们安排在X上执行操作zeta

E. Don''假设X''知道'关于预定的

操作。

在时间C我们删除bobble X.所有空间都被释放。


在时间D,我们分配了一个假的Y.如果有机会,Y和X有相同的

地址。


在时间E,该程序试图在X上执行预定的操作

。它必须能够检测到X不再存在,即使有b / b
使用X的地址。


只需将地址整理成一个整数就行不通,因为

地址是相同的。同样地使用不透明指针并不是
工作。这就是独特身份的重点。 -

不同的bobbles是不同的,即使它们具有相同的地址。


当然你可以使用序列号作为哈希表中的键或<某种搜索树,但与描述符/定位器配对结构相比,这相对来说计算成本相当昂贵。



Richard Harter, cr *@tiac.net
http://home.tiac.net/~cri http://www.varinoma.com

现在拯救地球!!

这是巧克力唯一的星球。

I may address the rest of your comments later (for which, thank
you very much) but this seems to be a serious point of confusion
so let me expand. What we can have is the following sequence of
events:

At time A we create bobble X. Space will be allocated for X as
needed, including a struct bobble_s.

At time B we schedule operation zeta to be performed on X at time
E. Don''t assume that X ''knows'' that about the scheduled
operation.

At time C we delete bobble X. All space is deallocated.

At time D we allocate bobble Y. As it chances, Y has the same
address as X.

At time E the program attempts to peform the scheduled operation
on X. It has to be able to detect that X is no longer there even
though there is a bobble around with X''s address.

Simply hashing the address into an integer won''t work because the
addresses are the same. Likewise using an opaque pointer doesn''t
work. That''s the point of the "unique id" - it is different for
different bobbles even though they have the same address.

Of course you can use sequence numbers as keys in a hash table or
some kind of search tree, but that is relatively computationally
expensive compared to the descriptor/locator paired structs.


Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It''s the only planet with chocolate.

推荐答案

在comp.lang.c中Richard Harter< cr*@tiac.netwrote:
In comp.lang.c Richard Harter <cr*@tiac.netwrote:

[...]

在A时刻,我们创建了一个挖空X. Space将被分配给X,因为需要

,包括一个struct bobble_s。
[ ... ]
At time A we create bobble X. Space will be allocated for X as
needed, including a struct bobble_s.



输入一个唯一的序列号在每个bobble中。在创建时,

从递增的全局计数器初始化序列号。

删除前,将其设置为零。不要释放()你删除的bobbles,

将它们放在一个免费列表中并从该列表中获取新的存储空间

你回到malloc()。这是为了确保序列号

为零或唯一,并且从不垃圾。

Put a unique "serial number" in each bobble. At creation time,
initialize the serial number from an incremented global counter.
Before deletion, set it to zero. Don''t free() your deleted bobbles,
put them on a free list and get new storage from that list before
you fall back on malloc(). This is to ensure that serial numbers
are either zero or unique, and never garbage.


在时间B我们安排操作zeta将在X时执行

E.不要认为X''知道'关于预定的

操作。
At time B we schedule operation zeta to be performed on X at time
E. Don''t assume that X ''knows'' that about the scheduled
operation.



在排队的请求中,存储序列号的副本以及

a指针指向X.


在时间C我们删除bobble X.所有空间都被释放。
At time C we delete bobble X. All space is deallocated.


在时间D我们分配bobble Y.如果有机会,Y与X有相同的

地址。
At time D we allocate bobble Y. As it chances, Y has the same
address as X.



Y获得一个全新的序列号。

Y gets a brand new serial number.


在时间E,程序尝试执行预定的操作< X上的
。它必须能够检测到X已经不再存在了,尽管有一个X'的地址。
At time E the program attempts to peform the scheduled operation
on X. It has to be able to detect that X is no longer there even
though there is a bobble around with X''s address.



检查序列号是否与请求中的副本匹配。

如果没有,那么只有你知道该怎么做。取消请求

或报告错误,适用于您的情况。


不要让全局计数器溢出。按照你的创作

和破坏纪律一丝不苟。这是C,语言

不会为你打电话析破坏者。


-

pa at panix dot com

Check that the serial number matches the copy on the request.
If not, well only you know what to do. Cancel the request
or report an error, whatever applies to your situation.

Don''t let the global counter overflow. Follow your creation
and destruction discipline meticulously. This is C, the language
won''t call destructors for you.

--
pa at panix dot com


Richard Harter写道:
Richard Harter wrote:

On Tue,2008年11月4日16:43:49 -0500,Eric Sosman

< Er ********* @sun.comwrote:
On Tue, 04 Nov 2008 16:43:49 -0500, Eric Sosman
<Er*********@sun.comwrote:

> Richard Harter写道:
>Richard Harter wrote:

>>> [...]
我要扔一个曲线球。我们的程序将包含对创建的bobbles的引用;我们不知道它们在哪里如此
当我们删除bobbles时它们会变得陈旧。代码需要能够检测到引用已经过时并且适当地处理它。
>>>[...]
I am going to throw in one curve ball. Our program will contain
references to created bobbles; we won''t know where these are so
they can go stale when we delete bobbles. The code will need to
be able to detect that a reference has gone stale and deal with
it appropriately.


据我所知,曲线与讨论的其余部分正交,所以我会忽略它。您的问题似乎涉及
表示隐藏和有效性检查,而不是垃圾收集。

As far as I can see, the curve is orthogonal to the rest of
the discussion, so I''ll ignore it. Your issues appear to deal with
representation hiding and validity checking, not with garbage
collection.



我可以稍后解决你的其他评论(为此,谢谢

非常多)但这似乎是一个严肃的观点混乱

所以让我扩展。我们可以拥有的是以下序列的

事件:


在时间A,我们创建bobble X. Space将被分配给X作为
需要
,包括一个struct bobble_s。


在时间B我们安排在X上执行操作zeta

E. Don''假设X''知道'关于预定的

操作。

在时间C我们删除bobble X.所有空间都被释放。


I may address the rest of your comments later (for which, thank
you very much) but this seems to be a serious point of confusion
so let me expand. What we can have is the following sequence of
events:

At time A we create bobble X. Space will be allocated for X as
needed, including a struct bobble_s.

At time B we schedule operation zeta to be performed on X at time
E. Don''t assume that X ''knows'' that about the scheduled
operation.

At time C we delete bobble X. All space is deallocated.



如果zeta仍然武装起来并且很危险,那么在我看来,这可能是程序中的错误而不是bobblefeature 。你要求一个被破坏的

bbble to记住它以前的存在不知何故,所以它可以

仍然以某种方式回应尝试用它来做一些事情它不存在的自我...它似乎没有对我来说,一个强大的

编程学科应该依赖来自

来世的精神信息。


如果你喜欢我,你的脚偶尔会比走楼梯更多一步。我从来没有想过它会检测我的失误并保护我

某种方式(通过部署一个带衬垫的细胞,或许?)特别是

如果楼梯本身已被拆除。


或者,正如教练曾经告诉我的那样,不要这样做。


-

Eric Sosman
es*****@ieee-dot-org.inva


2008年11月5日星期三02:55:29 +0000(UTC) , pa@see.signature.inva 盖子

(Pierre Asselin)写道:
On Wed, 5 Nov 2008 02:55:29 +0000 (UTC), pa@see.signature.invalid
(Pierre Asselin) wrote:

>在comp.lang.c中Richard Harter< cr*@tiac.netwrote:
>In comp.lang.c Richard Harter <cr*@tiac.netwrote:

> [... ]
在时间A,我们创建了一个浮动X.空间将被分配给X,因为需要包括一个struct bobble_s。
>[ ... ]
At time A we create bobble X. Space will be allocated for X as
needed, including a struct bobble_s.


放一个唯一的序列号在每个bobble中。在创建时,
从递增的全局计数器初始化序列号。
删除前,将其设置为零。不要释放()你删除的bobbles,
将它们放在一个免费列表中,然后从那个列表中获取新的存储空间,然后再回到malloc()上。这是为了确保序列号为零或唯一,并且从不垃圾。


Put a unique "serial number" in each bobble. At creation time,
initialize the serial number from an incremented global counter.
Before deletion, set it to zero. Don''t free() your deleted bobbles,
put them on a free list and get new storage from that list before
you fall back on malloc(). This is to ensure that serial numbers
are either zero or unique, and never garbage.


>在时间B我们安排操作zeta将在X上执行
E.不要以为X''知道'关于预定的
操作。
>At time B we schedule operation zeta to be performed on X at time
E. Don''t assume that X ''knows'' that about the scheduled
operation.


在排队的请求中,存储序列号的副本以及指向X的指针。


In your queued request, store a copy of the serial number as well as
a pointer to X.


>在时间C,我们删除了bobble X.所有空间都被释放。
>At time C we delete bobble X. All space is deallocated.


>在时间D我们分配bobble Y.如果有机会,Y与X具有相同的地址。
>At time D we allocate bobble Y. As it chances, Y has the same
address as X.


Y获得一个全新的序列号。


Y gets a brand new serial number.


>在时间E,程序尝试执行预定的操作
X.它必须能够检测到X不再存在,尽管有X的地址。
>At time E the program attempts to peform the scheduled operation
on X. It has to be able to detect that X is no longer there even
though there is a bobble around with X''s address.


检查序列号是否与请求中的副本匹配。
如果没有,那么只有你知道该怎么做。取消请求
或报告错误,无论适用于您的情况。

不要让全局计数器溢出。谨慎地遵循您的创作
和破坏纪律。这是C,语言
不会为你打电话析破坏者。


Check that the serial number matches the copy on the request.
If not, well only you know what to do. Cancel the request
or report an error, whatever applies to your situation.

Don''t let the global counter overflow. Follow your creation
and destruction discipline meticulously. This is C, the language
won''t call destructors for you.



呃,序列号方法是原始提案的一部分。

Richard Harter, cr *@tiac.net
http: //home.tiac.net/~cri http://www.varinoma.com

现在拯救地球!!

它是唯一一颗带巧克力的星球。

Er, the serial number approach was part of the original proposal.
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It''s the only planet with chocolate.


这篇关于Re:C中的编程技术问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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