event queue / checking if pointers are still valid

Discussion in 'C Programming' started by copx, Mar 24, 2006.

  1. copx

    copx Guest

    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?
    copx, Mar 24, 2006
    1. Advertisements

  2. I don't think there /is/ a way to ensure a C pointer is valid.
    Not really a C question, you may be better of in comp.programming.

    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.
    Define "elegant" and "queue".
    Vladimir S. Oka, Mar 24, 2006
    1. Advertisements

  3. copx

    copx Guest

    ...and that is the problem.
    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.
    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).
    copx, Mar 24, 2006
  4. copx

    Chris Dollin Guest

    There isn't one (in portable C).
    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).
    Well, yes. (I don't see what this has to do with event queues, mind.)
    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 Dollin, Mar 24, 2006
  5. [/QUOTE]
    Perhaps you should look into how those languages are implemented?

    -- Richard
    Richard Tobin, Mar 24, 2006
  6. copx

    copx Guest

    Perhaps you should look into how those languages are implemented?[/QUOTE]

    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.
    copx, Mar 24, 2006
  7. So it seems you already know you don't want to use C...
    Vladimir S. Oka, Mar 24, 2006
  8. Then you either live with it, or use a different language, I'm afraid.
    That doesn't make it a C question. Its really about an efficient
    algorithm for doing whatever it is you want to do.
    You could use one of those instead I guess, if this is a showstopper.

    Mark McIntyre
    Mark McIntyre, Mar 24, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.