473,788 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Mar 24 '06 #1
7 2875

copx wrote:
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.
I don't think there /is/ a way to ensure a C pointer is valid.
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.
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.
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?


Define "elegant" and "queue".

--
BR, Vladimir

Mar 24 '06 #2

"Vladimir S. Oka" <no****@btopenw orld.com> schrieb im Newsbeitrag
news:11******** *************@g 10g2000cwb.goog legroups.com...

copx wrote:
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.
I don't think there /is/ a way to ensure a C pointer is valid.


...and that is the problem.
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.


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


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.
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.


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).

Mar 24 '06 #3
copx wrote:
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.
There isn't one (in portable C).
The main issue is that the
objects an event data structure points two might be removed before the
event is executed.
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).
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.
Well, yes. (I don't see what this has to do with event queues, mind.)
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).


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.
Mar 24 '06 #4
In article <e0************ *@news.t-online.com>,
copx <in*****@invali d.com> wrote:
Not really a C question, you may be better of in comp.programmin g.
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.


Perhaps you should look into how those languages are implemented?

-- Richard
Mar 24 '06 #5

"Richard Tobin" <ri*****@cogsci .ed.ac.uk> schrieb im Newsbeitrag
news:e0******** ***@pc-news.cogsci.ed. ac.uk...
In article <e0************ *@news.t-online.com>,
copx <in*****@invali d.com> wrote:
Not really a C question, you may be better of in comp.programmin g.

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.


Perhaps you should look into how those languages are implemented?


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.


Mar 24 '06 #6

copx wrote:
"Richard Tobin" <ri*****@cogsci .ed.ac.uk> schrieb im Newsbeitrag
news:e0******** ***@pc-news.cogsci.ed. ac.uk...
In article <e0************ *@news.t-online.com>,
copx <in*****@invali d.com> wrote:
Not really a C question, you may be better of in comp.programmin g.

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.


Perhaps you should look into how those languages are implemented?


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.


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

--
BR, Vladimir

Mar 24 '06 #7
On Fri, 24 Mar 2006 13:00:59 +0100, in comp.lang.c , "copx"
<in*****@invali d.com> wrote:

"Vladimir S. Oka" <no****@btopenw orld.com> schrieb im Newsbeitrag
news:11******* **************@ g10g2000cwb.goo glegroups.com.. .

I don't think there /is/ a way to ensure a C pointer is valid.
..and that is the problem.


Then you either live with it, or use a different language, I'm afraid.
Not really a C question, you may be better of in comp.programmin g.


I do think it is a C question. Because my problems are caused by C-style
pointers.


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


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
Mar 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4207
by: Graeme Matthew | last post by:
Hi all I just cannot seem to find any documentation that shows an example of using the factory method Event() in threads. I have a thread pool and if there are no jobs in a Queue I want them to wait for something to be inserted. When a job is inserted I want to send an Event, the first thread that picks it up runs with the job the rest wait for another insert Event. I have been looking at some C, c++ implementations and some use a...
99
5199
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
18
2041
by: Elder Hyde | last post by:
Hey all, A class of mine needs to tell the outside world when its buffer is not empty. The problem is that C# seems to force you to put the event-raising code in the base class. To illustrate, consider what I'll do in Java: public interface DataAvailabilityListener extends java.util.EventListener { void dataArrived(DataAvailabilityEvent event); }
7
1510
by: Jeff Casbeer | last post by:
New to VB.. What is the VB syntax for posting a Windows event? For example, to have an event fire AFTER a form loads, I'd add a posted call to a "post_load" event, from "load". What is the VB syntax for that deferred call? Raiseevent seems to be synchronous, as does just calling the handler subroutine. Also, how do I defer processing to allow windows to process incomplete events, eg., painting Thanks - Jeff
19
6255
by: ern | last post by:
I need a FIFO queue of size 20, to keep track of a running average. Once the queue is full with 20 values, I want to take the sum/20 = AVERAGE. Then when a new value comes in, I want: (Old Sum - oldest value + newest value)/20 = New Average Anybody know an efficient way to implement that? Is a queue even the best way? Thanks,
6
2041
by: Abubakar | last post by:
Hi, lets say I have a connected SOCKET s. At some point in time, I want to know if the "s" is still valid, that it is still connected. Is there any API that I can give me this information? And can I register some callback like thing, that would inform me when "s" disconnection happens? What I usually do is while I call "send" or "recv", I get the socket_error and through that I know whats the status. But in this situation actually I...
9
2471
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the post event will always execuate after the system is completed... - I want to wrap this functionality in a framework, so I could possibly have 3 or 4 levels of inherited objects that need to have these pre / post events executed before and after the...
4
4599
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the length of my queue. I started getting compilation errors when I included a length function. <code> template<class ItemType> void Queue<ItemType>::MakeEmpty() {
8
2984
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of the big changes I want to make is event-driven code (rather than the linear flow I had in Java). I have spent a week or so searching Google, talking to a couple of programming friends, and just chewing on it in my brain. I think I have an ok handle...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.