Function Pointers Refined Memory Model 1 The C0 Memory Model so - - PowerPoint PPT Presentation

function pointers refined memory model
SMART_READER_LITE
LIVE PREVIEW

Function Pointers Refined Memory Model 1 The C0 Memory Model so - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Function Pointers

slide-2
SLIDE 2

Refined Memory Model

1

slide-3
SLIDE 3

The C0 Memory Model … so far

Two memories

 Local memory

  • one frame per function call
  • frame contains
  • its parameters
  • its local variables

 Allocated memory

  • arrays
  • pointer targets

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50

Allocated Memory Local Memory main hdict_lookup A H H k

“lime”

key_hash k

“lime” 5 3

Sample memory snapshot during the execution of an application that uses a hash dictionary

i

1 2

slide-4
SLIDE 4

A More Realistic Model

 Two distinct memories?

  • local memory and allocated memory

 But a computer has one memory

  • a large array of bytes indexed by

addresses

 C0 addresses are 64 bit long

  • 264 bytes
  • the smallest byte has address

0x0000000000000000

  • the largest byte has address

0xFFFFFFFFFFFFFFFF

0x0000000000000000 0xFFFFFFFFFFFFFFFF

Computer memory

This is 264-1

3

slide-5
SLIDE 5

A More Realistic Model

 Local and allocated memory are two segments in this memory

A

0xBB8

main hdict_lookup

H

0xD04

H

0xD04

k

“lime”

i

1

key_hash

k

“lime”

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50 5 3 0xD04 0xBB8 0x0 0xFF…FF

Allocated memory Local memory

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50

Allocated Memory Local Memory main hdict_lookup A H H k

“lime”

key_hash k

“lime”

5 3

i

1

4

slide-6
SLIDE 6

A More Realistic Model

 The segment where the allocated memory lives is called the heap

  • it contains a pile of data structures

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50 5 3 0xD04 0xBB8 0x0 0xFF…FF

Allocated memory

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50

Allocated Memory Local Memory

5 3

The HEAP

5

slide-7
SLIDE 7

A More Realistic Model

 The segment where the local memory lives is called the stack

  • function calls

make it grow and shrink like a stack

A

0xBB8

main hdict_lookup

H

0xD04

H

0xD04

k

“lime”

i

1

key_hash

k

“lime” 0x0 0xFF…FF

Local memory

Allocated Memory Local Memory main hdict_lookup A H H k

“lime”

key_hash k

“lime”

i

1

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50 5 3 0xD04 0xBB8

Allocated memory

The HEAP

0xD08 0xBB8

The STACK

6

slide-8
SLIDE 8

A More Realistic Model

 The stack grows downward

  • toward smaller addresses

 The heap grows upward

  • toward larger addresses
  • unless garbage collection has given

back existing heap space

 If they grow so much that they run into each other, we have a stack overflow

  • very rare with modern hardware
  • What about the rest of memory?

0xD04 0x0 0xFF…FF

Allocated memory Local memory

The STACK The HEAP

A

0xBB8

main hdict_lookup

H

0xD04

H

0xD04

k

“lime”

i

1

key_hash

k

“lime”

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50 5 3 0xBB8

7

slide-9
SLIDE 9

A More Realistic Model

 The top and bottom segments belong to the operating system  A C0 program cannot use them

  • it cannot read or write there
  • This is restricted memory
  • accessing it causes a

segmentation fault

 NULL is address 0x0000000000000000

  • a valid address that doesn’t

belong to the program

  • This is why dereferencing NULL

causes a segmentation fault OS

0xD04 0x0 0xFF…FF

Restricted Restricted Allocated memory Local memory

The STACK The HEAP

A

0xBB8

main hdict_lookup

H

0xD04

H

0xD04

k

“lime”

i

1

key_hash

k

“lime”

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50 5 3 0xBB8

OS

NULL What about this area?

8

slide-10
SLIDE 10

A More Realistic Model

 The CODE segment contains the compiled code of the program

  • every function has an address in

memory

  • the beginning of its binary

 This segment is read-only

  • writing to it causes a

segmentation fault

OS OS

main … hdict_lookup … key_hash … key_equiv … …

0xD04 0x0 0xFF…FF

Restricted Restricted Read-only Allocated memory Local memory

The STACK The HEAP CODE

A

0xBB8

main hdict_lookup

H

0xD04

H

0xD04

k

“lime”

i

1

key_hash

k

“lime”

1 2 3 4

“apple” 20 “pumpkin” 10 “banana” 50 5 3 0xBB8

Well, it’s got to live somewhere! What about this area?

9

slide-11
SLIDE 11

A More Realistic Model

 The TEXT segment contains all the string literals present in the program

  • not the strings constructed by

functions like string_join

  • those are hidden in the heap
  • every string has an address in

memory

  • the address of its first character
  • variables and fields of type string

contain this address

 This segment is read-only

OS OS

main … hdict_lookup … key_hash … key_equiv … … A

0xBB8

H

0xD04

H

0xD04

k

0x0AC

i

1

k

0x0AC 0x0AC

1 2 3 4

0x080 20 0x090 10 0x088 50 5 3 0xD04 0xBB8 0x0 0xFF…FF

Restricted Restricted Read-only Read-only Allocated memory Local memory

The STACK The HEAP CODE TEXT

main hdict_lookup key_hash

“apple” … “lime” …

TEXT and CODE are sometimes viewed as

  • ne segment called TEXT

Writing to it causes a segmentation fault

10

slide-12
SLIDE 12

A More Realistic Model

 This is not the end of the story!  Actual memory is much more complicated

  • This model will be significantly

refined in future classes

OS OS

main … hdict_lookup … key_hash … key_equiv … … “apple” … “lime” … A

0xBB8

H

0xD04

H

0xD04

k

0x0AC

i

1

k

0x0AC 0x0AC

1 2 3 4

0x080 20 0x090 10 0x088 50 5 3 0xD04 0xBB8 0x0 0xFF…FF

Restricted Restricted Read-only Read-only Allocated memory Local memory

The STACK The HEAP CODE TEXT

main hdict_lookup key_hash

Hint: no computer in existence comes even close to having 264 bytes of memory!

11

slide-13
SLIDE 13

Function Pointers

12

slide-14
SLIDE 14

Addresses a C0 Program can Use

 The address of an array

  • returned by alloc_array

 The address of a memory cell

  • returned by alloc

 NULL

  • that’s just address 0x00000000
  • but we can’t dereference it

 The address of a string

  • but C0 hides that they are even addresses

… and that’s it

13

slide-15
SLIDE 15

Addresses a C1 Program can Use

 Everything a C0 program can use

  • the address of an array
  • the address of a memory cell
  • NULL
  • the address of a string

 The address of a function

  • this is called a function pointer

14

slide-16
SLIDE 16

The language C1

 C1 is an extension of C0

  • Every C0 program is a C1 program

 C1 provides two additional mechanisms

  • Generic pointers
  • Function pointers

Both help with genericity

Rest of this unit

15

slide-17
SLIDE 17

The Address of a Function

 C1 provides the address-of operator to grab the address of a function

  • written “&”, prefix

 If key_hash starts at address 0x02A in the CODE segment, then &key_hash returns the address 0x02A

  • & can only be applied to the name of a

function in a C1 program

OS OS

main … hdict_lookup … key_hash … key_equiv … … “apple” … “lime” … A

0xBB8

H

0xD04

H

0xD04

k

0x0AC

i

1

k

0x0AC 0x0AC

1 2 3 4

0x080 20 0x090 10 0x088 50 5 3 0xD04 0xBB8 0x0 0xFF…FF

The STACK The HEAP CODE TEXT

main hdict_lookup key_hash

0x02A

C and other languages have many more uses for &

16

slide-18
SLIDE 18

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

  • 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

  • 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

slide-19
SLIDE 19

Function Types

 key_hash is a function that takes a string as input and returns an int  To give the name string_to_int_fn to the type of the functions that take a string as input and return an int, we write typedef int string_to_int_fn(string s);

  • thus, key_hash has type string_to_int_fn
  • and so does the <string> library function string_length

int key_hash(string s) { int len = string_length(s); int h = 0; for (int i = 0; i < len; i++) { h = h + char_ord(string_charat(s, i)); h = 1664525 * h + 1013904223; } return h; } Return type Type of the parameter Name chosen for the function type

18

slide-20
SLIDE 20

Function Types

typedef int string_to_int_fn(string s);

  • by convention, function types end in _fn
  • like types exported by a library interface end in _t
  • This is a different use of typedef from what we had in the past

 Function types are not functions

  • 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);

int key_hash(string s) { int len = string_length(s); int h = 0; for (int i = 0; i < len; i++) { h = h + char_ord(string_charat(s, i)); h = 1664525 * h + 1013904223; } return h; } Return type Type of the parameter Name chosen for the function type

19

slide-21
SLIDE 21

Function Types

 Any function can be given a type

  • the type of POW is defined as

typedef int binop_fn(int x, int y);

  • Steps to defining a function type
  • 1. Write down the prototype of the function

int POW(int x, int y);

  • 2. Write typedef in front of it

typedef int POW(int x, int y);

  • 3. Replace the function name with a type name of your choice

typedef int binop_fn (int x, int y);

  • Contracts can be included in the function type definition

typedef int binop_with_pos2_fn(int x, int y) /*@requires y >= 0; @*/ ;

int POW(int x, int y) //@requires y >= 0; { if (y == 0) return 1; return x * POW(x, y-1); }

20

slide-22
SLIDE 22

Storing Function Pointers

typedef int string_to_int_fn(string s); string_to_int_fn* F = &key_hash;

 F needs to be a pointer to a function that takes a string and returns an int

string_to_int_fn* F

  • a pointer to a string_to_int_fn
  • This is because &key_hash returns the address of key_hash
  • and this address is stored in F

 string_to_int_fn F = &key_hash; // no * is invalid C1 code

int key_hash(string s) { int len = string_length(s); int h = 0; for (int i = 0; i < len; i++) { h = h + char_ord(string_charat(s, i)); h = 1664525 * h + 1013904223; } return h; }

21

slide-23
SLIDE 23

Using Function Pointers

typedef int string_to_int_fn(string s); string_to_int_fn* F = &key_hash;

  • F contains the address of key_hash

 To call F on an input, we first need to dereference it

int h = (*F) ("hello");

  • Writing *F("hello") is incorrect
  • C1 interprets it as *(F("hello"))

 which doesn’t type check

  • Other languages have better syntax

int key_hash(string s) { int len = string_length(s); int h = 0; for (int i = 0; i < len; i++) { h = h + char_ord(string_charat(s, i)); h = 1664525 * h + 1013904223; } return h; } Applying a function pointer

22

slide-24
SLIDE 24

Safety of Function Pointers

 A function pointer is a pointer!

int h = (*F) ("hello");

is safe only if F != NULL  The address of the functions in a program are never NULL

  • Thus

string_to_int_fn* F = &key_hash; int h = (*F) ("hello"); is safe because F contains the address of key_hash

  • but

string_to_int_fn* F = NULL; int h = (*F) ("hello"); is unsafe

23

slide-25
SLIDE 25

Function Pointer Contracts

 The address of the functions in a program are never NULL  Function pointer operators have their own contracts

  • & always returns a non-NULL pointer

&f //@ensures \result != NULL;

  • where f is a function declared in the program
  • Only non-NULL function pointer can be called

(*F)(args) //@requires F != NULL;

This is the standard precondition for dereferencing a pointer

24