473,785 Members | 2,419 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
30 1962
James Kuyper <ja*********@ve rizon.netwrites :
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.
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().
No you can't (except that using a callback function for each
comparison is bound to impose some overhead). But I don't think Paul
did so. I suspect that he based his conclusion on observations of
actual qsort() implementations .

Whether Paul's conclusion is correct is a different matter.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 19 '07 #11
Paul Hsieh wrote:
>
.... snip ...
>
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.)
Utter bushwah. Qsort and similar operations are normally
precompiled, and will attempt to call a routine passed in via a
parameter. This cannot be a macro. All you will get is a bunch of
linkage errors or a total blowup, depending on the sophistication
of the linker.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Dec 20 '07 #12
CBFalconer <cb********@yah oo.comwrites:
Paul Hsieh wrote:
>>
... snip ...
>>
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.)

Utter bushwah. Qsort and similar operations are normally
precompiled, and will attempt to call a routine passed in via a
parameter. This cannot be a macro. All you will get is a bunch of
linkage errors or a total blowup, depending on the sophistication
of the linker.
Clearly the cmp operation used by qsort() can't be a macro, since it
has to be passed to qsort() as an argument of a pointer-to-function
type. I don't think Paul was claiming otherwise.

But if you write *your own* sort routine, as Paul suggests, then the
comparison routine can easily be a macro, or even simple inline code.
Such a sort routine would lack qsort()'s flexibility, in that it would
only be able to sort arrays of a single type.

qsort() pays for its flexibility (being able to sort arrays of any
type) with a loss of efficiency (due to the indirect calls to the
comparison routine). Sometimes this cost is worth paying, sometimes
it isn't. (A language with a template facility could give you the
best of both. Even in C, you *might* be able to define the entire
sorting routine as a macro, but it's not something I'd care to try.)

As for letting the comparison function have access to contextual
state, yes, that's a limitation of qsort(), and one that could be
fixed without losing its flexibility. But adding an extra required
parameter to the comparison routine would likely cause another small
loss of performance.

There are open-source C implementations of qsort(). If you need
something that the standard qsort() doesn't provide, you can always
grab one of them and modify it to your taste. Nothing in the C
standard says that qsort() is the only way to sort things.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 20 '07 #13
On Dec 20, 12:30 pm, Keith Thompson <ks...@mib.orgw rote:
CBFalconer <cbfalco...@yah oo.comwrites:
Paul Hsiehwrote:
... snip ...
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.)
Utter bushwah. Qsort and similar operations are normally
precompiled, and will attempt to call a routine passed in via a
parameter. This cannot be a macro. All you will get is a bunch of
linkage errors or a total blowup, depending on the sophistication
of the linker.

Clearly the cmp operation used by qsort() can't be a macro, since it
has to be passed to qsort() as an argument of a pointer-to-function
type. I don't think Paul was claiming otherwise.

But if you write *your own* sort routine, as Paul suggests, then the
comparison routine can easily be a macro, or even simple inline code.
Such a sort routine would lack qsort()'s flexibility, in that it would
only be able to sort arrays of a single type.
Really?

#define hsortT(type, context, array, length, cmpT) ... \
#define cmpT(type, context, el1, el2) ... \

I *DO* this all the time. The point is that this is the *MOST*
flexible solution. Even if you *wanted* to use function callbacks,
you just fill them into the macros:

#define typeCmp(type, context, el1, el2) cmpPOS (context, el1, el2)

void PaycostofcallOv erheadSort_type (
void * context,
type * array,
size_t len,
int (*cmpPOS) (void * context, type * el1, type * el2))
{
hsortT (type, context, array, len, typeCmp);
}

Of course, there's little point in doing this, but you can't claim its
less flexible than qsort.
qsort() pays for its flexibility (being able to sort arrays of any
type) with a loss of efficiency (due to the indirect calls to the
comparison routine). Sometimes this cost is worth paying, sometimes
it isn't.
Its worth paying if you don't care about performance. The only case
where qsort offers any compensating use is when in a single program
you are sorting very many, widely varied types, and you could care
less about its performance. (Maybe a proof of concept of something
running on an embedded system or something like that.)
[...] (A language with a template facility could give you the
best of both. Even in C, you *might* be able to define the entire
sorting routine as a macro, but it's not something I'd care to try.)
Why not? It really is not that hard. I mean, the only challenge is
writing a sort, which, for real computer programmers ... well it was
supposed to be an exercise in college. If you were asleep during
those classes, you can pull stuff from the web.
As for letting the comparison function have access to contextual
state, yes, that's a limitation of qsort(), and one that could be
fixed without losing its flexibility. But adding an extra required
parameter to the comparison routine would likely cause another small
loss of performance.
Thus maximizing the mediocrity of their solution. Its neither fast,
nor flexible.
There are open-source C implementations of qsort().
Lol! Ever watch the movie Office Space? Where they are looking up
the definition of "money laundering" in the dictionary? That's what
looking up open source for qsort() reminds me of.
[...] If you need
something that the standard qsort() doesn't provide, you can always
grab one of them and modify it to your taste. Nothing in the C
standard says that qsort() is the only way to sort things.
Sorting without a guarantee on the lower bound of performance and
necessarily paying an unnecessary overhead cost -- it makes you wonder
why computer science is even taught in universities at all.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Dec 20 '07 #14
Keith Thompson <ks***@mib.orgw rote:
>
qsort() pays for its flexibility (being able to sort arrays of any
type) with a loss of efficiency (due to the indirect calls to the
comparison routine).
There's no reason an implementation can't inline qsort() and once you do
that, you can also inline the comparison function.

-Larry Jones

My upbringing is filled with inconsistent messages. -- Calvin
Dec 21 '07 #15
la************@ siemens.com writes:
Keith Thompson <ks***@mib.orgw rote:
>qsort() pays for its flexibility (being able to sort arrays of any
type) with a loss of efficiency (due to the indirect calls to the
comparison routine).

There's no reason an implementation can't inline qsort() and once you do
that, you can also inline the comparison function.
[I'm piggybacking this followup off my own message, so the Reference
header isn't going to be quite right. Larry's followup appears on the
server where I read news, but not on the one where I post.]

Yes, I believe you're right.

I would think that inlining an indirect call (one made via a function
pointer parameter) would be more difficult than inlining an ordinary
call. Do any implementations actually do this?

Of course, it's still possible to write a qsort() call for which the
comparison function can't be inlined, for example by passing an
expression, rather than just a function name, for the compar
parameter. But a sufficiently clever compiler could certainly handle
that.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 21 '07 #16
Keith Thompson <ks***@mib.orgw rote:
>
I would think that inlining an indirect call (one made via a function
pointer parameter) would be more difficult than inlining an ordinary
call. Do any implementations actually do this?
In C, function calls are always made via a pointer. And in the case of
qsort(), the argument corresponding to the pointer parameter is nearly
always a function designator (i.e., the name of a function as opposed to
a pointer variable), so it shouldn't be any more difficult to inline
than any other function call. But I don't know of any implementations
that actually inline qsort(), presumably because its performance isn't
critical often enough to be worth while.

-Larry Jones

Mr. Subtlety drives home another point. -- Calvin
Dec 22 '07 #17
la************@ siemens.com writes:
Keith Thompson <ks***@mib.orgw rote:
>I would think that inlining an indirect call (one made via a function
pointer parameter) would be more difficult than inlining an ordinary
call. Do any implementations actually do this?

In C, function calls are always made via a pointer. And in the case of
qsort(), the argument corresponding to the pointer parameter is nearly
always a function designator (i.e., the name of a function as opposed to
a pointer variable), so it shouldn't be any more difficult to inline
than any other function call. But I don't know of any implementations
that actually inline qsort(), presumably because its performance isn't
critical often enough to be worth while.
[ Again, I'm piggybacking this onto my own article, since Larry's
article isn't on this server. It looks like Larry's articles aren't
showing up on aioe.org. Perhaps homeip.net, like rr.com, is under a
UDP? ]

Yes, but if it's simpler in assembly or machine code to call a
function directly than to call it indirectly via a pointer object or
value, then presumably the fact that ``func(42)'' involves a
conversion of ``func'' to a pointer goes away early in the compilation
process. C function calls are always done via pointers, but the
compiler doesn't have to treat them that way, as long as the same
effect is achieved.

qsort()'s comparison function argument is typically a simple function
designator, but if the qsort() call is inlined, then it's probably
going to look like the designator is converted to a pointer, then
copied to a local object (the parameter object), then used in an
indirect call. Of course any optimizer worth its salt will collapse
that into a simple call, which can then be inlined. So yes, I agree.

Going off on a bit of a tangent, paul Hsieh said elsewhere in this
thread that the overhead imposed by qsort() is acceptable only if you
don't care about performance. I disagree. In my opinion, the
overhead is acceptable if it doesn't significantly affect the
performance of the particular program you're using it in. The saving
in programming effort (avoiding the time needed either to write your
own sorting routine or find one elsewhere), and in risk (getting the
sorting routine wrong), often (but by no means always) can outweigh
the performance penalty.

And if you think that a sorting routine is simple enough that there's
no significant risk of getting it wrong, consider this (Jon Bentley,
"Programmin g Pearls"):

While the first binary search was published in 1946, the first
binary search that works correctly for all values of n did not
appear until 1962.

The most reliable code is the code that isn't there.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 22 '07 #18
On Dec 22, 1:04 pm, Keith Thompson <ks...@mib.orgw rote:
[...]
And if you think that a sorting routine is simple enough that there's
no significant risk of getting it wrong, consider this (Jon Bentley,
"Programmin g Pearls"):

While the first binary search was published in 1946, the first
binary search that works correctly for all values of n did not
appear until 1962.

The most reliable code is the code that isn't there.
So you think compiler vendors implement qsort() without any code?
qsort() also mashes things through void * pointers, which means you
*LOSE* type safety -- so this introduces other risk to the code.

All this is besides the point that you just equated sorting with
binary search.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Dec 27 '07 #19
Paul Hsieh <we******@gmail .comwrites:
On Dec 22, 1:04 pm, Keith Thompson <ks...@mib.orgw rote:
>[...]
And if you think that a sorting routine is simple enough that there's
no significant risk of getting it wrong, consider this (Jon Bentley,
"Programmin g Pearls"):

While the first binary search was published in 1946, the first
binary search that works correctly for all values of n did not
appear until 1962.

The most reliable code is the code that isn't there.

So you think compiler vendors implement qsort() without any code?
Of course not -- but it's code that *I* don't have to write. A bug in
a sort routine that I write (and test as much as I have time for) is
more likely than a bug in a vendor's qsort() routine that's been used
by zillions[*] of other programmers. That's not to say that a bug in a
vendor's code is impossible, of course.

I've never heard of a buggy qsort() in a released implementation of
the C standard library. Have you?
qsort() also mashes things through void * pointers, which means you
*LOSE* type safety -- so this introduces other risk to the code.
A valid point. You have to be careful in how you invoke qsort(), and
in how you write the comparison routine. I still think that using
qsort() is less error-prone than writing your own specialized sorting
routine (but sometimes the risk is worth it).
All this is besides the point that you just equated sorting with
binary search.
That was not my intent. I'd say that sorting and binary search are
tasks of comparable complexity. The observation that it took 16 years
to get binary search right was an illustration, not a proof.
[*] 1.21 zillion, on average, according to a recent survey.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 27 '07 #20

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

Similar topics

5
4439
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
11082
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
1428
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
9647
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
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10101
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
9959
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
8988
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...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
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
5396
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...
3
2893
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.