Lecture5

Lecture5 - 211 Computer Architecture Fall 2011 Abhishek...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Abhishek Bhattacharjee Topic: s Data representation 211: Computer Architecture Fall 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 2 C Review
Background image of page 2
Rutgers University Abhishek Bhattacharjee 3 Bug - # 1 scanf(“%d”, val );
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 4 Bug - # 2 Reading Uninitialized Memory s Assuming that heap data is initialized to zero /* return y = Ax */ int *matvec(int **A, int *x) { int *y = malloc(N*sizeof(int)); int i, j; for (i=0; i<N; i++) for (j=0; j<N; j++) y[i] += A[i][j]*x[j]; return y; }
Background image of page 4
Rutgers University Abhishek Bhattacharjee 5 Bug - # 3 Overwriting Memory s Allocating the (possibly) wrong sized object int **p; p = malloc(N*sizeof(int)); for (i=0; i<N; i++) { p[i] = malloc(M*sizeof(int)); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 6 Bug - # 4 Overwriting Memory s Off-by-one error int **p; p = malloc(N*sizeof(int *)); for (i=0; i<=N; i++) { p[i] = malloc(M*sizeof(int)); }
Background image of page 6
Rutgers University Abhishek Bhattacharjee 7 Bug - # 5 Overwriting Memory s Misunderstanding pointer arithmetic int *search(int *p, int val) { p += sizeof(int); return p; }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 8 Bug - # 6 Referencing Nonexistent Variables s Forgetting that local variables disappear when a function returns int *foo () { int val; return &val; }
Background image of page 8
Rutgers University Abhishek Bhattacharjee 9 Bug - # 7 Freeing Blocks Multiple Times x = malloc(N*sizeof(int)); <manipulate x> free(x); y = malloc(M*sizeof(int)); <manipulate y> free(x);
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 10 Bug - # 8 Referencing Freed Blocks x = malloc(N*sizeof(int)); <manipulate x> free(x); ... y = malloc(M*sizeof(int)); for (i=0; i<M; i++) y[i] = x[i]++;
Background image of page 10
Rutgers University Abhishek Bhattacharjee 11 Bug - # 9 Failing to Free Blocks (Memory Leaks) s Slow, long-term killer foo() { int *x = malloc(N*sizeof(int)); ... return; }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 12 Bug - # 10 Failing to Free Blocks (Memory Leaks) s Freeing only part of a data structure struct list { int val; struct list *next;}; foo() { struct list *head = malloc(sizeof(struct list)); head->val = 0; head->next = NULL; <create and manipulate the rest of the list> ... free(head); return; }
Background image of page 12
13 Abhishek Bhattacharjee Manipulate stored information Information is data s How is it represented? Basic information: numbers Human beings have represented numbers throughout history s Egyptian number system s Roman numeral Typically decimal s Natural for humans What Do Computer Do? Discoveregypt.com
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 10/31/2011 for the course 198 211 at Rutgers.

Page1 / 39

Lecture5 - 211 Computer Architecture Fall 2011 Abhishek...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online