UMBC CMSC 313, Computer Organization & Assembly Language, Spring 2013
Tuesday, April 9, 2013
Polymorphism in C
Assigned Reading in Null & Lobur:
Assigned Reading in Duntemann:
Slides (in PDF): none
- Example of a C program passing a function pointer to an
assembly language program (for Project 7):
- Here's the flip side, an example of assembly language program passing a function pointer
to a C program:
- Using void * pointers and function pointers to achieve genericity.
(Programs posted below.)
These are all different implementations of a simple singly linked list
- Version 1: plain vanilla linked list.
Each node is has an int data field.
- Version 2: uses #define constants DATA_TYPE
and DATA_TYPE_FORMAT for linked list of int.
- Version 3: this time define constants DATA_TYPE
and DATA_TYPE_FORMAT for linked list of double.
- Version 4: uses void * pointers that allow linked list
functions to point to different types of nodes. This works as long
as the next field is the first field in all the nodes.
Note that ll.c does not have to be recompiled to use it with
another node type.
- Version 5: This version allows us to have both int
and double nodes in one linked list. Each node now stores a
pointer to a function that knows how to print that node.
- Version 6: Previous version allowed only constant strings in
string nodes because of deallocation issues. In this version, each node
also has a delete function attached. Instead of storing another function
pointer in each node, we store a pointer to an array of function
pointers. Thus we have only one table for each type. Increasing the
number of functions in the table would not increase the size of each
- Version 7: Clean up the code and make header files for each
type of linked list. The implemenations can be compiled separately and
placed in a library using the ar command.
We also make a linked list node that points to a linked list.
(See ll_list.c and
This allows you to have linked lists of linked lists. Notice that
print_ll_node() and del_ll_node() can be implemented
quite simply using the linked list functions in ll.h.
28 Jan 2013 16:16:45 EST
to Spring 2013 CMSC 313 Homepage