event queue / checking if pointers are still valid

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

    #1

    event queue / checking if pointers are still valid

    How do you implement an event queue in C? The problem I had is that events
    needed pointers to the objects they affect and I do not know any way to
    check if pointers are actually valid in C. The main issue is that the
    objects an event data structure points two might be removed before the event
    is executed. The only solution I came up with was scanning the entire queue
    each time an object was destroyed to remove all references to it. That was
    rather ugly.
    Also serializing (i.e. saving to disk) pointer-based structures is horrible
    in C. You can not just save/restore pointers instead you have to translate
    them to something else. I usually translate them to index values which works
    but requires a lot of code (especially if you need to save/restore lots of
    pointers who point to data in different data structures).
    So what is the elegant way to implement an event queue in C?


  • Vladimir S. Oka

    #2
    Re: event queue / checking if pointers are still valid


    copx wrote:[color=blue]
    > How do you implement an event queue in C? The problem I had is that events
    > needed pointers to the objects they affect and I do not know any way to
    > check if pointers are actually valid in C.[/color]

    I don't think there /is/ a way to ensure a C pointer is valid.
    [color=blue]
    > The main issue is that the
    > objects an event data structure points two might be removed before the event
    > is executed. The only solution I came up with was scanning the entire queue
    > each time an object was destroyed to remove all references to it. That was
    > rather ugly.[/color]

    Not really a C question, you may be better of in comp.programmin g.

    Shooting from the hip: Why don't you construct your objects in a way
    that allows them to point to a queue member created for the event? That
    way, when destroying object you can destroy their events as well very
    easily.This becomes non-trivial, but still posible, if the object may
    be used by multiple events.
    [color=blue]
    > Also serializing (i.e. saving to disk) pointer-based structures is horrible
    > in C. You can not just save/restore pointers instead you have to translate
    > them to something else. I usually translate them to index values which works
    > but requires a lot of code (especially if you need to save/restore lots of
    > pointers who point to data in different data structures).
    > So what is the elegant way to implement an event queue in C?[/color]

    Define "elegant" and "queue".

    --
    BR, Vladimir

    Comment

    • copx

      #3
      Re: event queue / checking if pointers are still valid


      "Vladimir S. Oka" <novine@btopenw orld.com> schrieb im Newsbeitrag
      news:1143198273 .424600.67610@g 10g2000cwb.goog legroups.com...[color=blue]
      >
      > copx wrote:[color=green]
      >> How do you implement an event queue in C? The problem I had is that
      >> events
      >> needed pointers to the objects they affect and I do not know any way to
      >> check if pointers are actually valid in C.[/color]
      >
      > I don't think there /is/ a way to ensure a C pointer is valid.[/color]

      ...and that is the problem.
      [color=blue][color=green]
      >> The main issue is that the
      >> objects an event data structure points two might be removed before the
      >> event
      >> is executed. The only solution I came up with was scanning the entire
      >> queue
      >> each time an object was destroyed to remove all references to it. That
      >> was
      >> rather ugly.[/color]
      >
      > Not really a C question, you may be better of in comp.programmin g.[/color]

      I do think it is a C question. Because my problems are caused by C-style
      pointers. In many other languages object references are verifiable and some
      have built-in serialization too.
      [color=blue]
      > Shooting from the hip: Why don't you construct your objects in a way
      > that allows them to point to a queue member created for the event? That
      > way, when destroying object you can destroy their events as well very
      > easily.This becomes non-trivial, but still posible, if the object may
      > be used by multiple events.[/color]

      Objects being references by multiple events is the norm in my case. And in
      that case the suggested solution is even worse than just scanning the entire
      queue (given the fact that the queue is never THAT long).



      Comment

      • Chris Dollin

        #4
        Re: event queue / checking if pointers are still valid

        copx wrote:
        [color=blue]
        > How do you implement an event queue in C? The problem I had is that events
        > needed pointers to the objects they affect and I do not know any way to
        > check if pointers are actually valid in C.[/color]

        There isn't one (in portable C).
        [color=blue]
        > The main issue is that the
        > objects an event data structure points two might be removed before the
        > event is executed.[/color]

        That would be a mistake.

        Clearly, if a pending event may refer to objects, they /must not/ be
        removed until the event is dequeued (or itself deleted).
        [color=blue]
        > Also serializing (i.e. saving to disk) pointer-based structures is
        > horrible in C. You can not just save/restore pointers instead you have to
        > translate them to something else.[/color]

        Well, yes. (I don't see what this has to do with event queues, mind.)
        [color=blue]
        > I usually translate them to index values
        > which works but requires a lot of code (especially if you need to
        > save/restore lots of pointers who point to data in different data
        > structures).[/color]

        I'd take the same brute-force approach as above: if it's that hard,
        probably I'm building the wrong kind of data-structures: I should
        build ones that I can (de)serialise conveniently.

        As usual, specific examples are easier to argue about than abstractions.

        --
        Chris "x.f(y) == f(x, y) == (x, y).f" Dollin
        The shortcuts are all full of people using them.

        Comment

        • Richard Tobin

          #5
          Re: event queue / checking if pointers are still valid

          In article <e00mss$4ud$02$ 1@news.t-online.com>,
          copx <invalid@invali d.com> wrote:
          [color=blue][color=green]
          >> Not really a C question, you may be better of in comp.programmin g.[/color][/color]
          [color=blue]
          >I do think it is a C question. Because my problems are caused by C-style
          >pointers. In many other languages object references are verifiable and some
          >have built-in serialization too.[/color]

          Perhaps you should look into how those languages are implemented?

          -- Richard

          Comment

          • copx

            #6
            Re: event queue / checking if pointers are still valid


            "Richard Tobin" <richard@cogsci .ed.ac.uk> schrieb im Newsbeitrag
            news:e00qif$2bn 9$2@pc-news.cogsci.ed. ac.uk...[color=blue]
            > In article <e00mss$4ud$02$ 1@news.t-online.com>,
            > copx <invalid@invali d.com> wrote:
            >[color=green][color=darkred]
            >>> Not really a C question, you may be better of in comp.programmin g.[/color][/color]
            >[color=green]
            >>I do think it is a C question. Because my problems are caused by C-style
            >>pointers. In many other languages object references are verifiable and
            >>some
            >>have built-in serialization too.[/color]
            >
            > Perhaps you should look into how those languages are implemented?[/color]

            AFAIK all those languages are very high-level and use garbage
            collection/automatic memory management. That is why they are able to know if
            object references are valid or not I think. Adding all this to C is a bit
            much and I do not want to use garbage collection either.








            Comment

            • Vladimir S. Oka

              #7
              Re: event queue / checking if pointers are still valid


              copx wrote:[color=blue]
              > "Richard Tobin" <richard@cogsci .ed.ac.uk> schrieb im Newsbeitrag
              > news:e00qif$2bn 9$2@pc-news.cogsci.ed. ac.uk...[color=green]
              > > In article <e00mss$4ud$02$ 1@news.t-online.com>,
              > > copx <invalid@invali d.com> wrote:
              > >[color=darkred]
              > >>> Not really a C question, you may be better of in comp.programmin g.[/color]
              > >[color=darkred]
              > >>I do think it is a C question. Because my problems are caused by C-style
              > >>pointers. In many other languages object references are verifiable and
              > >>some
              > >>have built-in serialization too.[/color]
              > >
              > > Perhaps you should look into how those languages are implemented?[/color]
              >
              > AFAIK all those languages are very high-level and use garbage
              > collection/automatic memory management. That is why they are able to know if
              > object references are valid or not I think. Adding all this to C is a bit
              > much and I do not want to use garbage collection either.[/color]

              So it seems you already know you don't want to use C...

              --
              BR, Vladimir

              Comment

              • Mark McIntyre

                #8
                Re: event queue / checking if pointers are still valid

                On Fri, 24 Mar 2006 13:00:59 +0100, in comp.lang.c , "copx"
                <invalid@invali d.com> wrote:
                [color=blue]
                >
                >"Vladimir S. Oka" <novine@btopenw orld.com> schrieb im Newsbeitrag
                >news:114319827 3.424600.67610@ g10g2000cwb.goo glegroups.com.. .[color=green]
                >>
                >> I don't think there /is/ a way to ensure a C pointer is valid.[/color]
                >
                >..and that is the problem.[/color]

                Then you either live with it, or use a different language, I'm afraid.[color=blue][color=green]
                >> Not really a C question, you may be better of in comp.programmin g.[/color]
                >
                >I do think it is a C question. Because my problems are caused by C-style
                >pointers.[/color]

                That doesn't make it a C question. Its really about an efficient
                algorithm for doing whatever it is you want to do.
                [color=blue]
                >In many other languages object references are verifiable and some
                >have built-in serialization too.[/color]

                You could use one of those instead I guess, if this is a showstopper.

                Mark McIntyre
                --
                "Debugging is twice as hard as writing the code in the first place.
                Therefore, if you write the code as cleverly as possible, you are,
                by definition, not smart enough to debug it."
                --Brian Kernighan

                Comment

                Working...