473,770 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

smuggling data in and out of alarm handlers and the like

Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.

Is there a better way of doing this than the three methods described below?

1. use global variables. This is fine for small monolithic programs
but isn't great in larger programs using lots of code in separate
files and/or shared libraries.

2. create a function like this:

SOME_STRUCT *smuggle_pointe r(SOME_STRUCT *pointer){
static SOME_STRUCT *hold_pointer;
if(pointer)hold _pointer = pointer;
return(hold_poi nter);
}

and load it with data before any possible calls to the
problem functions. The target function contains:

SOME_STRUCT *pointer;
pointer = smuggle_pointer (NULL);

which gives that function access to this data.

3. use setenv/getenv. This is not part of ANSI C, but is
usually available. Only useful for one or two simple
values.
Is there a better way?

Thanks,

David Mathog
Dec 14 '07 #1
30 1961
David Mathog <ma****@caltech .eduwrites:
Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.
For qsort and bsearch, I use replacements that take an extra void
* parameter and pass it to the comparison function. The void *
can be used to pass arbitrary data. (There's really no excuse
for these functions failing to provide any way to pass auxiliary
data. They are badly designed in my opinion.)
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Dec 14 '07 #2
David Mathog wrote:
>
Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.
When there's only a small number of values
that your variable can have:

For example,
if you had an array of pointers
to strings of a few columns,
and you wanted to qsort the strings
based on an arbitrary column,

then you could define an array of pointers to
different compar functions,
which you would also have to write,
and then a variable indexing the array of function pointers
would be part of the qsort call.

--
pete
Dec 14 '07 #3
David Mathog wrote:
Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort.
Such functions are typically called "callback functions". Just to clarify.
In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.
Yeah, that's a botch. For the qsort() callback you can at least pass in an
array of pointers to structs that have an additional member which points to
your "smuggled" data, which is clumsy because it eats memory and
performance.

When I started programmming GUIs using GTK+ (which revolves around callback
functions), it at first struck me as odd that each and every callback had
some curious "void *user_data" parameter which seemed useless. Boy, is it
not, as I realized after about 10 minutes. It's odd that the designers of
the C (and other) libraries didn't think of this mecessity.

robert
Dec 17 '07 #4
Robert Latest <bo*******@yaho o.comwrote:

# When I started programmming GUIs using GTK+ (which revolves around callback
# functions), it at first struck me as odd that each and every callback had
# some curious "void *user_data" parameter which seemed useless. Boy, is it
# not, as I realized after about 10 minutes. It's odd that the designers of
# the C (and other) libraries didn't think of this mecessity.

This is a primitive way of doing closures. The full notion of a closure
came after the beginning of C and Unix. Proper implementation of closures
requires garbage collection which many in C are still reluctant to use.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I love the smell of commerce in the morning.
Dec 17 '07 #5
On Dec 14, 9:03 am, David Mathog <mat...@caltech .eduwrote:
Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.

Is there a better way of doing this than the three methods described below?
Yeah, the methods you describe are all basically "global" in one
fashion or another, which prevents re-entrancy. The right answer to
use interfaces that always carry a "user pointer" around with it --
something opaque to the calling mechanism, but known to the originator
and target function.

By this criteria, qsort() is basically broken since the cmp() function
has no simple way to gain access to the contextual state.
Fortunately, its fairly easy and straightforward to write your own
sort. (Probably *more* fortunately, being forced to do so will make
you realize that the sort function should be a macro, not a function
call. This allows your "cmp" to itself be a macro, and thus eliminate
the major cost of qsort(), and you can pass contextual parameters to
macros just as easily as to functions.)

Thus, if its possible, just make sure some extra pointer to your
context is passed, and received in your leaf functions and demand that
your interface/transport pass along those pointers. In the case of a
generic transport, you should make it a void *, otherwise if its
custom you can declare the pointer exactly.

If you are stuck with an interface that didn't see fit to pass along
that extra pointer, then you can try to hide your context in a data
pointer, but of course that doesn't work for something like qsort.
But again, we don't care about qsort, since you should never use it
anyways (its always slow, always algorithmically sub-optimal, and
always inflexible -- qsort is just one of many examples of where its
clear that the C-library was designed by amateurs.)

If you *have* to use global variables, then you are going to want to
shield it from re-entrancy or multiple threads with some sort of
mutex, then just use a global or static depending on your taste (but
its like waxing a beat-up jalopy).

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Dec 18 '07 #6
Paul Hsieh wrote:
But again, we don't care about qsort, since you should never use it
anyways (its always slow, always algorithmically sub-optimal, and
always inflexible -- qsort is just one of many examples of where its
clear that the C-library was designed by amateurs.)
I can't find the passage in the C standard that requires the qsort function
to be implemented in some way or another.

robert
Dec 19 '07 #7
Paul Hsieh wrote:
On Dec 14, 9:03 am, David Mathog <mat...@caltech .eduwrote:
>Every so often I find a need to move data in or out of functions which
are not called directly. For instance, alarm handlers or the compare
function used by qsort. In these cases there is no possibility of
passing the data as a function parameter, so it needs to be "smuggled"
in.

Is there a better way of doing this than the three methods described below?

Yeah, the methods you describe are all basically "global" in one
fashion or another, which prevents re-entrancy. [...]

If you *have* to use global variables, then you are going to want to
shield it from re-entrancy or multiple threads with some sort of
mutex, then just use a global or static depending on your taste (but
its like waxing a beat-up jalopy).
Note that qsort() itself is not guaranteed to be reentrant
(7.1.4p4), so the use or non-use of static variables doesn't
really change the picture. For a multi-threaded program, this
means you either need to rely on standards that go beyond C's,
or else use some kind of mutual exclusion as Paul suggests. Even
for single-threaded programs (the only kind C really understands),
this means that qsort()'s comparison function must not use a
subsidiary qsort(). Similarly, bsearch()'s comparator mustn't
use bsearch().

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 19 '07 #8
Robert Latest wrote:
Paul Hsieh wrote:
>But again, we don't care about qsort, since you should never use it
anyways (its always slow, always algorithmically sub-optimal, and
always inflexible -- qsort is just one of many examples of where its
clear that the C-library was designed by amateurs.)

I can't find the passage in the C standard that requires the qsort
function to be implemented in some way or another.

robert
Nevertheless it's interface cannot be changed, which is what, I think,
Paul Hsieh was talking about.

Dec 19 '07 #9
santosh wrote:
Robert Latest wrote:
>Paul Hsieh wrote:
>>But again, we don't care about qsort, since you should never use it
anyways (its always slow, always algorithmically sub-optimal, and
always inflexible -- qsort is just one of many examples of where its
clear that the C-library was designed by amateurs.)
I can't find the passage in the C standard that requires the qsort
function to be implemented in some way or another.

robert

Nevertheless it's interface cannot be changed, which is what, I think,
Paul Hsieh was talking about.
You can't derive the conclusion that qsort() is "... always slow, always
algorithmically sub-optimal, ..." from the interface specification, or
indeed from anything else that the standard says about qsort().
Dec 19 '07 #10

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

Similar topics

5
4438
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or MBM http://mbm.livewiredev.com/ can be used. Both of the mentioned apps expose data got from the hardware in a shared memory area.
2
1292
by: Rolf | last post by:
Hallo here I like to read a external data file.( text file ) But the data is not consistent. As you see in 1 line ther is the word 'ceasing' but not in the other
1
1982
by: Sergey | last post by:
How to send alarm to a thread? I can set alarm in main thread, but how then send exception to another thread to wake it if it executes too long?
7
11081
by: Adrian Casey | last post by:
I have a multi-threaded python application which uses pexpect to connect to multiple systems concurrently. Each thread within my application is a connection to a remote system. The problem is when one of the child threads runs a command which generates an unlimited amount of output. The classic example of this is the "yes" command. If you execute "pexpect.run('yes')", your cpu will sit at 100% forever. Here is a simple multi-threaded...
0
869
by: CharChabil | last post by:
Hey guys I m reading from a text file (that contains data like the following) What I want is to simply get the data of each block separatly Any hint for that? A1/EXT "BK82 LB73 21233" 105 061018 1804 EXTERNAL ALARM COOLING ALARM A2
3
3522
by: ankitks | last post by:
Hi guys, is there any utility available as a protection against endless_loop() something like this: alarm.set(5); //set timeout for 5 sec endless_loop(); alarm.reset(); //reset it to 0, as alarm not needed any more
1
8725
by: albertleng | last post by:
Hi all... Can anyone give clue in doing below? Let say i have a table A with the following 3 fields. 1)Date 2)Title 3)Status. i need to insert into table B (also have the same 3 fields) from this table A with the condition where Title is "Alarm" and Status is "ON". This can be done by a simple "INSERT" query.
0
1271
by: thelightkeeper | last post by:
Hi, in MSSQL, I have 2 tables naming , --------------------------------------- EquipmentID : nvarchar(50), FK, reference .EquipmentID AlarmID : nvarchar(50), PK AlarmHappenTime : Datetime ,required, not null AlarmClearTime : Datetime, required, not null
2
1427
by: Knoxy | last post by:
Hi guys, I'm using a gridview to do some inline editing. The data binds okay, its goes into edit mode and cancel okay. I'm using my own custom commandName, "save", and it picks this up okay. Don't want to use the built in "update" command. Trouble is when I go to access the form fields (textboxes etc), there isn't any data there (ie text in the textbox), I can access them though, I guess I'm missing an event somewhere. I've stripped...
0
9618
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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
10038
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
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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.