D
David Mathog
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_pointer(SOME_STRUCT *pointer){
static SOME_STRUCT *hold_pointer;
if(pointer)hold_pointer = pointer;
return(hold_pointer);
}
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
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_pointer(SOME_STRUCT *pointer){
static SOME_STRUCT *hold_pointer;
if(pointer)hold_pointer = pointer;
return(hold_pointer);
}
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