UMBC CMSC 313, Computer Organization & Assembly Language, Fall 2012
Tuesday, November 6, 2012
Polymorphism in C
Assigned Reading in Null & Lobur:
Assigned Reading in Duntemann:
Preview Slides (in PDF): none
- Example of assembly language program passing a function pointer
to a C program (for Project 7):
- 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.
2 Nov 2012 14:25:31 EDT
to Fall 2012 CMSC 313 Homepage