Lecture5 - 211 Computer Architecture Fall 2011 Abhishek...

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

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

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

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

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

View Full Document Right Arrow Icon
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; }
Image of page 4
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)); }
Image of page 5

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

View Full Document Right Arrow Icon
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)); }
Image of page 6
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; }
Image of page 7

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

View Full Document Right Arrow Icon
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; }
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);
Image of page 9

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

View Full Document Right 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]++;
Image of page 10
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; }
Image of page 11

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

View Full Document Right Arrow Icon
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; }
Image of page 12
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
Image of page 13

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

View Full Document Right Arrow Icon
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
Image of page 14
Rutgers University
Image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern