[LCP]global variable alternative

Matthew Sanderson matthew at DaMOO.csun.edu
Thu Apr 4 16:48:05 UTC 2002


Mark,

I'm not sure what you mean by dynamic calls - I'm going to assume you mean
'late binding' a la C++ virtual member functions. C doesn't have this
built into the language, but there are ways you can accomplish the same
things. Your choices as I see them are:

1. Mimic a C++ virtual function table using a C array of function pointers
and make indirect calls through these function pointers. The 'virtual
function table' will probably be stored inside a struct which thus mimics
a C++ class; the 'constructor' for the 'class' sets entries in the VFT,
over-riding the default values (the parent 'class's versions of these
functions). So you can even have polymorphism of a sort. I think if you
even consider doing this, you should move to C++ rather than try to force
C to be C++. I did some of this once, because C++ was not an option due to
portability concerns; it became such an ugly kluge (and a *bastard* to 
debug with all the indirection) that I can't honestly recommend it.

for example, something along the lines of:

struct parent_class {
    int (*func2)(void);
    int (*func1)(int);
} parent = {
    parent_func1,
    parent_func2
};

struct child_class {
    struct parent_class parent;
    [...derived 'class' stuff here]
};

struct child_class * child_class_create(...)
{
    struct child_class * kid = [malloc...]
    kid->parent.func2 = child_func2;
    return kid;
}

2. Use a dynamic-link-library library such as the LoadLibrary etc APIs
under Win32, shlib_xxx under HPUX, dlopen/dlsym etc under Solaris, Linux
and other UNIXes. The library exports a whole lot of symbols by name,
which effectively make up the entries in the VFT, so that the loaded
library is a 'class'. When you load the library you use dlsym or whatever
to fill the VFT.

There are probably other ways to get late binding, or the appearance of it
or the advantages of it, but it all depends on what you're trying to do...
IMNSHO it'd be more efficient to remove the need for late binding, or
remove the need to use C and not C++, than either of the above kluges.

HTH

--m@

On Wed, 3 Apr 2002, Mike & Penny Novack wrote:

> Joachim,
>     The question is sort of "philosophical" and related to ALL
> programming languages (not specifically C).
> 
>     How much of a "purist" do you want to be? What's your philospophy
> with regard to structured programming and functions being "pure
> functions"?
> 
>     The "right" (ie: purist approach) this structure becomes an extra
> parameter to all functions.
> 
>     The "practical" programmer is likely to reason thusly. Once the
> structure has been initialized by the main process (or some
> initialization process which reads the user database and fills in the
> preferences) it never changes. In other words, it is from that point on
> "constant" data used by all the other functions to determine their
> behavior. Thus having those functions access it as a "global" makes
> sense.
> 
>     Mike
> 
> PS -- There are OTHER consderations which can come into play. Right now
> you are learning C and presumably all your links are "static" (ie: these
> functions are all LINKED and so references can be resolved). If working
> with a language where these might be DYNAMIC calls of functions (or
> prcesses) compiled separately and NOT linked together you would have to
> pass (the address of) the structure as an additional parameter.
> 
> PSS -- Anyone out there be able to explain to ME if/how C ever gets
> involved with something like "dynamic calls"?
> 
> 
> 
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List
> 





More information about the linuxCprogramming mailing list