C_lecture_5

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

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

CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap

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

View Full Document
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 */
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) == ? */

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

View Full Document
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!
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);*/

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

View Full Document
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]
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; }

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

View Full Document
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; }
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!

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

View Full Document
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++; }
Arrays and pointers (6) We replaced 3 pointer additions with three pointer increments, which are usually faster Even more significant for 2-d arrays

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

View Full Document
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)
Dynamic memory allocation (2) Let's say we want to allocate memory for e.g.

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