Re: A question of programming technique in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Harter

    Re: A question of programming technique in C

    On Wed, 05 Nov 2008 18:58:39 +0000, Ben Bacarisse
    <ben.usenet@bsb .me.ukwrote:
    >cri@tiac.net (Richard Harter) writes:
    [snip]
    >
    >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.
    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 Bacarisse

    #2
    Re: A question of programming technique in C

    cri@tiac.net (Richard Harter) writes:
    On Wed, 05 Nov 2008 18:58:39 +0000, Ben Bacarisse
    <ben.usenet@bsb .me.ukwrote:
    <snip>
    >>... What I don't get is why the a more normal "double
    >>indirect" solution does not work for you:
    >>
    <snip some diagrams>
    >>
    >>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.
    >
    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.
    Yes, that was also clear to me but I can't see how your organisation
    avoids this problem. I am sure that is my fault, but I don't see how
    right now.

    --
    Ben.

    Comment

    • Richard Harter

      #3
      Re: A question of programming technique in C

      On Thu, 06 Nov 2008 03:56:30 +0000, Ben Bacarisse
      <ben.usenet@bsb .me.ukwrote:
      >cri@tiac.net (Richard Harter) writes:
      >
      >On Wed, 05 Nov 2008 18:58:39 +0000, Ben Bacarisse
      ><ben.usenet@bs b.me.ukwrote:
      ><snip>
      >>>... What I don't get is why the a more normal "double
      >>>indirect" solution does not work for you:
      >>>
      ><snip some diagrams>
      >>>
      >>>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.
      >>
      >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.
      >
      >Yes, that was also clear to me but I can't see how your organisation
      >avoids this problem. I am sure that is my fault, but I don't see how
      >right now.
      The key observation is that although you can never free a
      locator, you can reuse it you have unique ids, whereas you can't
      if you only have a single pointer.

      So, create an array of locators. Make the pointer in the locator
      a void *. Maintain a free list of locators, i.e., locators not
      bound to a current bobble. Use the pointer as a link field for
      the free list of unbound locators, and as a bobble address for
      the bound locators. The size of the locator array is
      proportional to the maximum number of bobbles existing at one
      time.

      You don't have to do it that way, of course. There are
      alternatives for reusing the locators.


      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

      Working...