Reasoning About Code 1/25/2010 int deref(int *p) { return *p; } - - PowerPoint PPT Presentation

reasoning about code
SMART_READER_LITE
LIVE PREVIEW

Reasoning About Code 1/25/2010 int deref(int *p) { return *p; } - - PowerPoint PPT Presentation

Reasoning About Code 1/25/2010 int deref(int *p) { return *p; } /* requires: p != NULL */ int deref(int *p) { return *p; } int sum(int a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) total += a[i]; return total; } /*


slide-1
SLIDE 1

Reasoning About Code

1/25/2010

slide-2
SLIDE 2

int deref(int *p) { return *p; }

slide-3
SLIDE 3

/* requires: p != NULL */ int deref(int *p) { return *p; }

slide-4
SLIDE 4

int sum(int a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) total += a[i]; return total; }

slide-5
SLIDE 5

/* requires: a != NULL && size(a) >= n */ int sum(int a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) total += a[i]; return total; }

slide-6
SLIDE 6

/* requires: a != NULL && size(a) >= n */ int sum(int a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) total += a[i]; return total; }

slide-7
SLIDE 7

/* requires: a != NULL && size(a) >= n */ int sum(int a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) /* 0 <= i && i < n && n <= size(a) */ total += a[i]; return total; }

slide-8
SLIDE 8

int sumderef(int *a[], size_t n) { int total = 0, i; for (i=0; i<n; i++) total += *(a[i]); return total; }

slide-9
SLIDE 9

int sumderef(int *a[], size_t n) { int total = 0, i; for (i=0; i<n; i++) total += *(a[i]); return total; } Woops! If (int)n < 0, i becomes negative, and a[i] is unsafe.

slide-10
SLIDE 10

int sumderef(int *a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) total += *(a[i]); return total; }

slide-11
SLIDE 11

/* requires: a != NULL && size(a) >= n && ??? */ int sumderef(int *a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) total += *(a[i]); return total; }

slide-12
SLIDE 12

/* requires: a != NULL && size(a) >= n && for all j in 0..n-1, a[j] != NULL */ int sumderef(int *a[], size_t n) { int total = 0; for (size_t i=0; i<n; i++) total += *(a[i]); return total; }

slide-13
SLIDE 13

void *mymalloc(size_t n) { void *p = malloc(n); if (!p) { perror("malloc"); exit(1); } return p; }

slide-14
SLIDE 14

/* ensures: retval != NULL */ void *mymalloc(size_t n) { void *p = malloc(n); if (!p) { perror("malloc"); exit(1); } return p; }

slide-15
SLIDE 15

char *tbl[N]; int hash(char *s) { int h = 17; while (*s) h = 257*h + (*s++) + 3; return h % N; } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-16
SLIDE 16

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; while (*s) h = 257*h + (*s++) + 3; return h % N; } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-17
SLIDE 17

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; /* 0 <= h */ while (*s) h = 257*h + (*s++) + 3; return h % N; } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-18
SLIDE 18

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; /* 0 <= h */ while (*s) /* 0 <= h */ h = 257*h + (*s++) + 3; return h % N; } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-19
SLIDE 19

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; /* 0 <= h */ while (*s) /* 0 <= h */ h = 257*h + (*s++) + 3; /* 0 <= h */ return h % N; } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-20
SLIDE 20

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; /* 0 <= h */ while (*s) /* 0 <= h */ h = 257*h + (*s++) + 3; /* 0 <= h */ return h % N; /* 0 <= retval < N */ } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-21
SLIDE 21

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; /* 0 <= h */ while (*s) /* 0 <= h */ h = 257*h + (*s++) + 3; /* 0 <= h */ return h % N; /* 0 <= retval < N */ } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-22
SLIDE 22

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; /* 0 <= h */ while (*s) /* 0 <= h */ h = 257*h + (*s++) + 3; /* 0 <= h */ return h % N; /* 0 <= retval < N */ } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-23
SLIDE 23

char *tbl[N]; /* ensures: 0 <= retval && retval < N */ int hash(char *s) { int h = 17; /* 0 <= h */ while (*s) /* 0 <= h */ h = 257*h + (*s++) + 3; /* 0 <= h */ return h % N; /* 0 <= retval < N */ } bool search(char *s) { int i = hash(s); return tbl[i] && (strcmp(tbl[i], s)==0); }

slide-24
SLIDE 24

char *getcomment(char *src, size_t srclen) { size_t n = (src[0]<<8) + src[1]; size_t clen = n – 2; char *comment = malloc(clen+1); memcpy(comment, src, clen); comment[clen] = ‘\0’; return comment; }