Professional Documents
Culture Documents
Practical Programming in C - LECTURE 9-6-087-January-Iap-2010 MIT6 - 087IAP10 - Lec09
Practical Programming in C - LECTURE 9-6-087-January-Iap-2010 MIT6 - 087IAP10 - Lec09
Review
Using External Libraries
Symbols and Linkage
Static vs. Dynamic Linkage
Linking External Libraries
Symbol Resolution Issues
Creating Libraries
Data Structures
B-trees
Priority Queues
p r i n t f ( "%d %f\n" , ( i n t ) px , ( f l o a t ) p f ) ;
or
strings)
int ret = fp( str1 , str2 );
or
Hash table (or hash map): array of linked lists for storing
Review
Using External Libraries
Symbols and Linkage
Static vs. Dynamic Linkage
Linking External Libraries
Symbol Resolution Issues
Creating Libraries
Data Structures
B-trees
Priority Queues
compile time
Symbols linked to denitions in external libraries during
linking
Our own program produces symbols, too
p u t s ( msg ) ;
r e t u r n 0 ;
p u t s ( msg ) ;
r e t u r n 0 ;
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
athena% nm hello.o
Output:
0000000000000000 T main
0000000000000000 R msg
U puts
symbol
Addresses all zero before linking; symbols not allocated
memory yet
Undened symbols are dened externally, resolved during
linking
1
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it..
stdio.h?
Compiler doesnt bother creating symbols for unused
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
athena%1 nm hello
Output:
..
0000000000400524 T main
000000000040062c R msg
U puts@@GLIBC_2.2.5
Addresses for static (allocated at compile time) symbols
Symbol puts located in shared library GLIBC_2.2.5 (GNU
C standard library)
Shared symbol puts not assigned memory until run time
1
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
10
before use
Can allocate at compile time (static) or at run time (shared)
Advantages/disadvantages to both
Symbols in same le, other .o les, or static libraries
11
Static linkage
..
.
00000000004014c0 W puts
..
.
0000000000400304 T main
..
.
000000000046cd04 R msg
..
.
W: linked to another dened symbol
1
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
12
Static linkage
dynamic 10K)
Resulting executable does not depend on locating external
13
Dynamic linkage
shared libraries
Shared library symbols loaded as part of program startup
(before main())
Requires external library to dene symbol exactly as
expected from header le declaration
changing function in shared library can break your program
version information used to minimize this problem
reason why common libraries like libc rarely modify or
14
to resulting executable
15
ld.so
Whenever shared libraries on system change, need to run
16
17
symbols in library:
int dlclose(void handle);
18
19
19
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
20
Review
Using External Libraries
Symbols and Linkage
Static vs. Dynamic Linkage
Linking External Libraries
Symbol Resolution Issues
Creating Libraries
Data Structures
B-trees
Priority Queues
21
Creating libraries
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
21
athena% ldconfig -v
1
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
22
Review
Using External Libraries
Symbols and Linkage
Static vs. Dynamic Linkage
Linking External Libraries
Symbol Resolution Issues
Creating Libraries
Data Structures
B-trees
Priority Queues
23
Data structures
algorithms
B-tree - generalized binary search tree, used for databases
and le systems
Priority queue - ordering data by priority, used for sorting,
23
B-tree structure
least t, up to 2t)
Tree is balanced all leaves at same level
Node contains list of keys divide range of elements in
children
Initializing a B-tree
node), no keys
Note: root node exempt from minimum children
requirement
25
Inserting elements
26
Inserting elements
27
Inserting elements
28
Searching a B-tree
29
Deletion
30
Deletion examples
31
Deletion examples
32
Deletion examples
33
Priority queue
highest priority
Common implementations: heap or binary search tree
Operations: insertion, peek/extract max-priority element,
34
Heaps
priority(parent)
More sophisticated heaps exist e.g. binomial heap,
Fibonacci heap
Well focus on simple binary heaps
Usually implemented as an array with top element at
beginning
Can sort data using a heap O(n log n) worst case
in-place sort!
35
Extracting data
top of heap
Can peek by looking at top element
Can remove top element, move last element to top, and
36
37
38
Summary
Topics covered:
Using external libraries
Creating libraries
Data structures
B-tree
priority queue
39
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.