C_lecture_5 - CS 11 C track lecture 5 Last week pointers...

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

View Full Document Right Arrow Icon
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap
Image of page 1

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

View Full Document Right Arrow Icon
Pointers (from last week) Address: location where data stored Pointer: variable that holds an address int i = 10; int *j = &i; int k = 2 * (*j); /* deref j */
Image of page 2
Pointer arithmetic (1) Can add/subtract integers to/from pointers int i[5] = { 1, 2, 3, 4, 5 }; int *j = i; /* (*j) == ? */ j++; /* (*j) == ? */ j += 2; /* (*j) == ? */ j -= 3; /* (*j) == ? */
Image of page 3

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

View Full Document Right Arrow Icon
Pointer arithmetic (2) Get size of a type using the sizeof operator: printf("size of integer: %d\n", sizeof (int)); printf("size of (int *): %d\n", sizeof (int *)); N.B. sizeof is not a function takes a type name as an argument!
Image of page 4
Pointer arithmetic (3) N.B. pointer arithmetic doesn't add/subtract address directly but in multiples of the size of the type in bytes int i[] = { 1, 2, 3, 4, 5 }; int *j = i; j++; /* means: j = j + 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
Arrays and pointers (1) Arrays are pointers in disguise! Arrays: "syntactic sugar" for pointers int i[5] = {1, 2, 3, 4, 5}; printf("i[3] = %d\n", i[3]); printf("i[3] = %d\n", *(i + 3)); i[3] and *(i + 3) are identical! i is identical to &i[0]
Image of page 6
Arrays and pointers (2) Can use pointer arithmetic instead of array operations Can be faster! int array[1000]; for (i = 1; i < 998; i++) { array[i] = (array[i-1] + array[i] + array[i+1]) / 3.0; }
Image of page 7

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

View Full Document Right Arrow Icon
Arrays and pointers (3) Exactly the same as: int array[1000]; for (i = 1; i < 998; i++) { *(array+i) = (*(array+i-1) + *(array+i) + *(array+i+1)) / 3.0; }
Image of page 8
Arrays and pointers (4) When you say *(array + i) , you have to add i to array and dereference but incrementing pointers by 1 is faster!
Image of page 9

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

View Full Document Right Arrow Icon
Arrays and pointers (5) int array[1000]; int *p1, *p2, *p3; p1=array; p2=array+1; p3=array+2; for (i = 1; i < 998; i++) { *p2 = (*p1 + *p2 + *p3) / 3.0; p1++; p2++; p3++; }
Image of page 10
Arrays and pointers (6) We replaced 3 pointer additions with three pointer increments, which are usually faster Even more significant for 2-d arrays
Image of page 11

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

View Full Document Right Arrow Icon
Dynamic memory allocation (1) Recall that we can't do this: int n = 10; int arr[ n ]; /* not legal C */ However, often want to allocate an array where size of array not known in advance This is known as "dynamic memory allocation" dynamic as opposed to "static" (size known at compile time)
Image of page 12
Dynamic memory allocation (2) Let's say we want to allocate memory for e.g.
Image of page 13

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

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