reasoning about code
play

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; } /*


  1. Reasoning About Code 1/25/2010

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

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

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

  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; }

  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; }

  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; }

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

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

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

  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; }

  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; }

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

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

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

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

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

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

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

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

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

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

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

  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; }

Recommend


More recommend