1 / 17

Common bugs

Common bugs. CS 2204 Class meeting 10. Example 1. TYPE: Accidental for (i=0; i<numrows; i++) for (j=0; j<numcols; j++); pixels++;. Example 2. TYPE: Missing or improper initialization int minval(int *A, int n) { int currmin; for (int i=0; i<n; i++) if (A[i] < currmin)

custerj
Download Presentation

Common bugs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Common bugs CS 2204 Class meeting 10

  2. Example 1 • TYPE: Accidental for (i=0; i<numrows; i++) for (j=0; j<numcols; j++); pixels++;

  3. Example 2 • TYPE: Missing or improper initialization int minval(int *A, int n) { int currmin; for (int i=0; i<n; i++) if (A[i] < currmin) currmin = A[i]; return currmin; }

  4. Example 3 • TYPE: Dyslexic int minval(int *A, int n) { int currmin = MAXINT; for (int i=0; i<n; i++) if (A[i] > currmin) currmin = A[i]; return currmin; }

  5. Example 4 • TYPE: copy and paste bug switch (i) { case 1: do_something(1); break; case 2: do_something(2); break; case 3: do_something(1); break; case 4: do_something(4); break; default: break; }

  6. Example 5 • TYPE: Accidental if (foo = 5) foo == 7;

  7. Example 6 • TYPE: Abused global int i = 5; int j; int foo(int j) { for (i=0; i<j; i++) do_nothing(); return j; } void ineedj(void) { cout << "j is " << j << "\n"; } main() { int j; j = foo(i); ineedj(); }

  8. Example 7 • TYPE: Macro bug // random returns a random (positive) integer. // Random returns a random integer in the range 0 // to n-1. #define Random(n) random()%n val = Random(j-i+1);

  9. Example 8 • TYPE: model error char* string1 = "Hello"; char* string2 = "World"; if (string1 == string2) do_something();

  10. Example 9 • TYPE: Memory Error int i; char string[5] = "hello"; int j;

  11. Program memory

  12. Example 10 • TYPE: Memory error char* ptr; cin >> ptr;

  13. Example 11 • TYPE: Off-by-one error int i; int array[5]; int j; for (i=0; i<=5; i++) cin >> array[i];

  14. Example 12 • TYPE: Special case error // Delete the node following the one that ptr is // pointing at. void del_link(lnode* ptr) { ptr->next = ptr->next->next; }

  15. Example 13 • TYPE: Stack frame error char *initialize() { char string[80]; char* ptr = string; return ptr; } main() { char *myval = initialize(); do_something_with(myval); }

  16. Example 14 • TYPE: Stack frame error char* assign() { return "hello world!"; } main() { char *ptr = assign(); }

  17. Example 15 • TYPE: Static Vs dynamic data main() { Record city; lnode *list = NULL; while (data_to_read()) { Readin_data(&city); insert(&city, &list); } } void insert(Record*& city, lnode*& list) { lnode* ptr = new lnode; ptr->next = list; list = ptr; prt->data = city; }

More Related