Function Pointers
Refined Memory Model 1
The C0 Memory Model … so far Local Memory Allocated Memory Two memories main A “apple” 20 Local memory H o one frame per function call 5 3 “pumpkin” 10 o frame contains hdict_lookup 0 its parameters H 1 its local variables 2 k “lime” 3 Allocated memory i 4 1 “banana” 50 o arrays key_hash o pointer targets k “lime” Sample memory snapshot during the execution of an application that uses a hash dictionary 2
0xFFFFFFFFFFFFFFFF A More Realistic Model Two distinct memories? local memory and allocated memory But a computer has one memory o a large array of bytes indexed by Computer addresses memory C0 addresses are 64 bit long 2 64 bytes o the smallest byte has address 0x0000000000000000 o the largest byte has address 0xFFFFFFFFFFFFFFFF This is 2 64 -1 0x0000000000000000 3
0xFF…FF A More Realistic Model main A 0xBB8 H 0xD04 hdict_lookup H 0xD04 Local Local and allocated memory are k “lime” memory i 1 two segments in this memory key_hash k “lime” Local Memory Allocated Memory 0xBB8 main “apple” 20 0xD04 “apple” 20 A 5 3 “pumpkin” 10 Allocated 0 H 5 3 1 “pumpkin” 10 memory 2 hdict_lookup 3 0 H 4 “banana” 50 1 “lime” k 2 i 1 3 4 key_hash “banana” 50 “lime” k 0x0 4
0xFF…FF A More Realistic Model The segment where the allocated memory lives is called the heap o it contains a pile of data structures Allocated Memory 0xBB8 “apple” 20 0xD04 5 3 “apple” 20 “pumpkin” 10 Allocated The 0 1 memory HEAP 5 3 Local Memory 2 “pumpkin” 10 3 4 “banana” 50 0 1 2 3 4 “banana” 50 0x0 5
0xFF…FF A More Realistic Model main A 0xBB8 H 0xD04 hdict_lookup H The 0xD04 Local The segment where the k “lime” memory STACK i 1 local memory lives is key_hash k “lime” called the stack o function calls make it grow and shrink Local Memory 0xBB8 like a stack “apple” 20 0xD04 main 5 3 A 0xBB8 “pumpkin” 10 Allocated Memory Allocated The 0 1 memory H 0xD08 HEAP 2 3 hdict_lookup 4 “banana” 50 H “lime” k i 1 key_hash “lime” k 0x0 6
0xFF…FF A More Realistic Model main A 0xBB8 H 0xD04 hdict_lookup H The 0xD04 Local The stack grows downward k “lime” memory STACK i 1 o toward smaller addresses key_hash k “lime” The heap grows upward o toward larger addresses unless garbage collection has given back existing heap space 0xBB8 “apple” 20 0xD04 5 3 If they grow so much that they “pumpkin” 10 Allocated The 0 1 memory HEAP run into each other, we have a 2 3 stack overflow 4 “banana” 50 very rare with modern hardware o What about the rest of memory? 0x0 7
0xFF…FF OS Restricted A More Realistic Model main A 0xBB8 H 0xD04 hdict_lookup H The 0xD04 Local The top and bottom segments k “lime” memory STACK i 1 belong to the operating system key_hash k “lime” A C0 program cannot use them it cannot read or write there o This is restricted memory 0xBB8 o accessing it causes a “apple” 20 0xD04 5 3 “pumpkin” 10 segmentation fault Allocated The 0 1 memory HEAP 2 3 NULL is address 4 “banana” 50 0x0000000000000000 o a valid address that doesn’t What about this area? belong to the program This is why dereferencing NULL causes a segmentation fault OS Restricted NULL 0x0 8
0xFF…FF OS Restricted A More Realistic Model main A 0xBB8 H 0xD04 hdict_lookup H The 0xD04 Local The CODE segment contains k “lime” memory STACK i 1 the compiled code of the key_hash k “lime” program Well, it’s got to live somewhere! 0xBB8 “apple” 20 o every function has an address in 0xD04 5 3 “pumpkin” 10 Allocated The memory 0 1 memory HEAP the beginning of its binary 2 3 4 “banana” 50 What about this area? This segment is read-only main … o writing to it causes a hdict_lookup … CODE Read-only key_hash … segmentation fault key_equiv … … OS Restricted 0x0 9
0xFF…FF OS Restricted A More Realistic Model main A 0xBB8 H 0xD04 hdict_lookup H The 0xD04 Local The TEXT segment contains k 0x0AC memory STACK i 1 all the string literals present in key_hash k 0x0AC the program o not the strings constructed by functions like string_join those are hidden in the heap 0xBB8 o every string has an address in 0x080 20 0xD04 5 3 0x090 10 memory Allocated The 0 1 memory the address of its first character HEAP 2 3 variables and fields of type string 4 0x088 50 contain this address TEXT “apple” … Read-only “lime” … 0x0AC main … This segment is read-only hdict_lookup … CODE Read-only key_hash … key_equiv … … Writing to it causes TEXT and CODE are a segmentation fault sometimes viewed as OS Restricted one segment called TEXT 0x0 10
0xFF…FF OS Restricted A More Realistic Model main A 0xBB8 H 0xD04 hdict_lookup H The 0xD04 Local k 0x0AC memory STACK i 1 key_hash k 0x0AC This is not the end of the story! Actual memory is much more complicated 0xBB8 0x080 20 0xD04 o This model will be significantly 5 3 0x090 10 Allocated The 0 refined in future classes 1 memory HEAP 2 3 4 0x088 50 TEXT “apple” … Read-only “lime” … 0x0AC Hint: no computer in existence main … comes even close to having hdict_lookup … CODE 2 64 bytes of memory! Read-only key_hash … key_equiv … … OS Restricted 0x0 11
Function Pointers 12
Addresses a C0 Program can Use The address of an array o returned by alloc_array The address of a memory cell o returned by alloc NULL that’s just address 0x00000000 o but we can’t dereference it The address of a string o but C0 hides that they are even addresses … and that’s it 13
Addresses a C1 Program can Use Everything a C0 program can use o the address of an array o the address of a memory cell o NULL o the address of a string The address of a function o this is called a function pointer 14
The language C1 C1 is an extension of C0 o Every C0 program is a C1 program C1 provides two additional mechanisms o Generic pointers o Function pointers Rest of this unit Both help with genericity 15
0xFF…FF OS The Address of a Function main A 0xBB8 H 0xD04 hdict_lookup H The 0xD04 C1 provides the address-of operator k 0x0AC STACK i 1 to grab the address of a function key_hash k 0x0AC o written “&”, prefix If key_hash starts at address 0x02A in the CODE segment, then 0xBB8 0x080 20 0xD04 5 3 &key_hash 0x090 10 The 0 1 returns the address 0x02A HEAP 2 3 o & can only be applied to the name of a 4 0x088 50 TEXT “apple” … function in a C1 program “lime” … 0x0AC main … CODE hdict_lookup … C and other languages key_hash … 0x02A have many more uses key_equiv … for & … OS 0x0 16
What to do with a Function Pointer? Eventually, we want to apply the function it points to to some arguments But first, we generally store a function pointer o in a variable F = &key_hash; F is a variable containing a function pointer what is its type? all C0/C1 variables have a type o but also in a data structure p->hash = &key_hash; p->hash is a field containing a function pointer what is its type? struct fields, array elements, memory cells have a type in C0/C1 17
Function Types key_hash is a function that takes int key_hash(string s) { int len = string_length(s); a string as input and returns an int int h = 0; for (int i = 0; i < len; i++) { h = h + char_ord(string_charat(s, i)); To give the name string_to_int_fn h = 1664525 * h + 1013904223; } to the type of the functions that return h; take a string as input and return } an int, we write typedef int string_to_int_fn (string s); Return type Type of the parameter Name chosen for the function type o thus, key_hash has type string_to_int_fn o and so does the <string> library function string_length 18
int key_hash(string s) { int len = string_length(s); Function Types int h = 0; for (int i = 0; i < len; i++) { h = h + char_ord(string_charat(s, i)); h = 1664525 * h + 1013904223; } return h; typedef int string_to_int_fn (string s); } Return type Type of the parameter Name chosen for the function type o by convention, function types end in _fn like types exported by a library interface end in _t o This is a different use of typedef from what we had in the past Function types are not functions o we cannot write string_to_int_fn("hello") We can give a function type any name we want typedef int string_hash_fn(string s); 19
Recommend
More recommend