On Wed, 05 Nov 2008 18:58:39 +0000, Ben Bacarisse
<ben.usenet@bsb .me.ukwrote:
[snip]
Your presentation is very clear, thank you. Of course you could
use a pair of pointers. The problem is that you can never
release the locator unless you can guarantee that there are no
extant descriptors pointing to it. This creates a memory leak.
This may not matter; then again it may. I prefer code that
doesn't have memory leaks.
If that isn't clear, consider an environment in which the number
of bobbles is bounded, but which is continually creating and
deleting bobbles. Over time the number of locators grows
unboundedly.
You can avoid the leak by scanning all descriptors and deleting
all locators that no longer have a reference, but that doesn't
strike me as a sane tactic in C. It would be a different matter
if we were talking about a language with garbage collection but
we're not.
Richard Harter, cri@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
<ben.usenet@bsb .me.ukwrote:
>cri@tiac.net (Richard Harter) writes:
>
>I could not find anything to snip so there it is. Sorry. If I follow
>you, you are making a structure a bit like this:
>
>
descriptor | locator bobble
+--------+ | +--------+ +--------+
| ----+-------->| ----+--------->|internal|
| id: 34 | | | id: 34 | | data |
+--------+ | +--------+ +--------+
|
user code | internal to the bobble implementation
>
>so that when a bobble is destroyed we get this picture:
>
descriptor | locator
+--------+ | +--------+
| ----+-------->| |
| id: 34 | | | id: 0 |
+--------+ | +--------+
>
>so as to be able to detect invalid accesses. I understand that the
>first arrow may not be a pointer but some other kind of reference (you
>suggest an index). What I don't get is why the a more normal "double
>indirect" solution does not work for you:
>
descriptor | locator bobble
+--------+ | +--------+ +--------+
| ----+-------->| ----+--------->|internal|
+--------+ | +--------+ | data |
| +--------+
>
>so that when a bobble is deleted, we simply set the second pointer to
>NULL:
>
descriptor | locator
+--------+ | +--------+
| ----+-------->| NULL |
+--------+ | +--------+
>
>I think everything would be clearer if you could say why this is not
>the right solution for your bobbles. The big advantage is that,
>outside, there is simply a pointer -- and a pointer to an opaque
>object at that.
>I could not find anything to snip so there it is. Sorry. If I follow
>you, you are making a structure a bit like this:
>
>
descriptor | locator bobble
+--------+ | +--------+ +--------+
| ----+-------->| ----+--------->|internal|
| id: 34 | | | id: 34 | | data |
+--------+ | +--------+ +--------+
|
user code | internal to the bobble implementation
>
>so that when a bobble is destroyed we get this picture:
>
descriptor | locator
+--------+ | +--------+
| ----+-------->| |
| id: 34 | | | id: 0 |
+--------+ | +--------+
>
>so as to be able to detect invalid accesses. I understand that the
>first arrow may not be a pointer but some other kind of reference (you
>suggest an index). What I don't get is why the a more normal "double
>indirect" solution does not work for you:
>
descriptor | locator bobble
+--------+ | +--------+ +--------+
| ----+-------->| ----+--------->|internal|
+--------+ | +--------+ | data |
| +--------+
>
>so that when a bobble is deleted, we simply set the second pointer to
>NULL:
>
descriptor | locator
+--------+ | +--------+
| ----+-------->| NULL |
+--------+ | +--------+
>
>I think everything would be clearer if you could say why this is not
>the right solution for your bobbles. The big advantage is that,
>outside, there is simply a pointer -- and a pointer to an opaque
>object at that.
use a pair of pointers. The problem is that you can never
release the locator unless you can guarantee that there are no
extant descriptors pointing to it. This creates a memory leak.
This may not matter; then again it may. I prefer code that
doesn't have memory leaks.
If that isn't clear, consider an environment in which the number
of bobbles is bounded, but which is continually creating and
deleting bobbles. Over time the number of locators grows
unboundedly.
You can avoid the leak by scanning all descriptors and deleting
all locators that no longer have a reference, but that doesn't
strike me as a sane tactic in C. It would be a different matter
if we were talking about a language with garbage collection but
we're not.
Richard Harter, cri@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Comment