Lecture5

# Lecture5 - 211 Computer Architecture Fall 2011 Abhishek...

This preview shows pages 1–16. Sign up to view the full content.

Abhishek Bhattacharjee Topic: square6 Data representation 211: Computer Architecture Fall 2011

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

View Full Document
Rutgers University Abhishek Bhattacharjee 2 C Review
Rutgers University Abhishek Bhattacharjee 3 Bug - # 1 scanf(“%d”, val );

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

View Full Document
Rutgers University Abhishek Bhattacharjee 4 Bug - # 2 Reading Uninitialized Memory square6 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; }
Rutgers University Abhishek Bhattacharjee 5 Bug - # 3 Overwriting Memory square6 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)); }

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

View Full Document
Rutgers University Abhishek Bhattacharjee 6 Bug - # 4 Overwriting Memory square6 Off-by-one error int **p; p = malloc(N*sizeof(int *)); for (i=0; i<=N; i++) { p[i] = malloc(M*sizeof(int)); }
Rutgers University Abhishek Bhattacharjee 7 Bug - # 5 Overwriting Memory square6 Misunderstanding pointer arithmetic int *search(int *p, int val) { while (*p && *p != val) p += sizeof(int); return p; }

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

View Full Document
Rutgers University Abhishek Bhattacharjee 8 Bug - # 6 Referencing Nonexistent Variables square6 Forgetting that local variables disappear when a function returns int *foo () { int val; return &val; }
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);

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

View Full Document
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]++;
Rutgers University Abhishek Bhattacharjee 11 Bug - # 9 Failing to Free Blocks (Memory Leaks) square6 Slow, long-term killer foo() { int *x = malloc(N*sizeof(int)); ... return; }

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

View Full Document
Rutgers University Abhishek Bhattacharjee 12 Bug - # 10 Failing to Free Blocks (Memory Leaks) square6 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; }
Rutgers University 13 Abhishek Bhattacharjee Manipulate stored information Information is data square6 How is it represented? Basic information: numbers Human beings have represented numbers throughout history square6 Egyptian number system square6 Roman numeral Typically decimal square6 Natural for humans What Do Computer Do? Discoveregypt.com

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

View Full Document
Rutgers University Abhishek Bhattacharjee 14 Number System Comprises of square6 Set of numbers or elements square6 Operations on them square6 Rules that define properties of operations Need to assign value to numbers Let us take decimal square6 Base 10 square6 Numbers are written as d n ...d 2 d 1 d 0 square6 Each digit is in [0-9] square6 Value of a number is interpreted as = × n i i i d 0 10
Rutgers University

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern