Saturday, February 16, 2013

Static and Dynamic Linkage in C Programming

A subclass inherits the statically linked methods of its superclass and it can choose
to inherit or overwrite the dynamically linked methods. Consider the declarations
for move() and draw():
void move (void * point, int dx, int dy);
void draw (const void * self);
We cannot discover the linkage from the two declarations, although the implementation
of move() does its work directly, while draw() is only the selector function
which traces the dynamic linkage at runtime. The only difference is that we declare
a statically linked method like move() as part of the abstract data type interface in
Point.h, and we declare a dynamically linked method like draw() with the memory
management interface in new.h, because we have thus far decided to implement
the selector function in new.c.
Static linkage is more efficient because the C compiler can code a subroutine
call with a direct address, but a function like move() cannot be overwritten for a
subclass. Dynamic linkage is more flexible at the expense of an indirect call — we
have decided on the overhead of calling a selector function like draw(), checking
the arguments, and locating and calling the appropriate method. We could forgo
the checking and reduce the overhead with a macro* like
#define draw(self)
((* (struct Class **) self) —> draw (self))
but macros cause problems if their arguments have side effects and there is no
clean technique for manipulating variable argument lists with macros. Additionally,
the macro needs the declaration of struct Class which we have thus far made available
only to class implementations and not to the entire application.
Unfortunately, we pretty much decide things when we design the superclass.
While the function calls to the methods do not change, it takes a lot of text editing,
possibly in a lot of classes, to switch a function definition from static to dynamic
linkage and vice versa.we will use a simple preprocessor to
simplify coding, but even then linkage switching is error-prone.

No comments:

Post a Comment