lecture12 - CIS 450 Computer Architecture and Organization...

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

View Full Document Right Arrow Icon
CIS 450 Computer Architecture and Organization Lecture 12: Pointers and Arrays Mitch Neilsen Mitch Neilsen [email protected] [email protected] 219D Nichols Hall 219D Nichols Hall
Background image of page 1

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

View Full DocumentRight Arrow Icon
–2– Arrays Arrays Familiar definition: type name[size] Familiar usage: name[index] Differences from Java: ± name is not an object ± name is like a constant “pointer” Array index lookups are not bounds checked: name[size+10000] isn’t an error! Address cannot be changed at run-time because name is a constant integer address value
Background image of page 2
–3– Arrays Arrays Arrays are not objects Arrays have a: ± Type ([#] of T) ± Size (sizeof(T)*size) ± Base address (name)
Background image of page 3

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

View Full DocumentRight Arrow Icon
–4– Other Array Operations Other Array Operations *array : the value of the first element in the array (array + i) : adjust the base address to a new address pointing to the (i+1)th element (indexing starts at 0) *(array + i) : the value of the (i+1)th element in the array array[ i ] = *(array + i)
Background image of page 4
–5– Array Operations Array Operations x[ i ] = *(x + i) x[ i ] is “syntactic sugar”: looks nicer but offers no additional functionality (x + i) is called “pointer arithmetic” * is the dereference operator and means: “take next type of” (possibly changing the expression value too)
Background image of page 5

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

View Full DocumentRight Arrow Icon
–6– Dereferencing Dereferencing The dereference operator * is sometimes called the “ contents of operator which strictly speaking is incorrect. Mr. T says, “Don’t fall into this trap, sucka. Don’t be a foo’.”
Background image of page 6
–7– Array Example Array Example Walk through array1.cpp
Background image of page 7

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

View Full DocumentRight Arrow Icon
–8– Example: Array1.cpp Example: Array1.cpp sizeof(x sizeof(x ) = 16 ) = 16 sizeof(int sizeof(int ) = 4 ) = 4 x[0] = 1 x[0] = 1 x[1] = 2 x[1] = 2 x[2] = 3 x[2] = 3 x[3] = 4 x[3] = 4 x = 0xbfd28b10 x = 0xbfd28b10 x + 0 = 0xbfd28b10 x + 0 = 0xbfd28b10 x + 1 = 0xbfd28b14 x + 1 = 0xbfd28b14 x + 2 = 0xbfd28b18 x + 2 = 0xbfd28b18 x + 3 = 0xbfd28b1c x + 3 = 0xbfd28b1c .. .. const const int int size = 4; size = 4; int int main() main() { int int x[size x[size ]; ]; char char ch ch ; for ( for ( int int i = 0; i < size; ++i) i = 0; i < size; ++i) x[i ] = i + 1; ] = i + 1; cout cout << " << " sizeof(x sizeof(x ) = " << ) = " << sizeof(x sizeof(x ) << ) << endl endl ; cout cout << " << " sizeof(int sizeof(int ) = " << ) = " << sizeof(int sizeof(int ) << ) << endl endl ; for ( for ( int int i = 0; i < size; ++i) i = 0; i < size; ++i) cout cout << "x[" << i << "] = " << << "x[" << i << "] = " << x[i x[i ] << ] << endl endl ; cout cout << "x = " << x << endl ; for ( for ( int int i = 0; i < size; ++i) i = 0; i < size; ++i) cout cout << "x + " << i << " = " << x + i << << "x + " << i << " = " << x + i << endl endl ;
Background image of page 8
–9– Example: Array1.cpp (cont.) Example: Array1.cpp (cont.) *(x + 0) = 1 *(x + 0) = 1 *(x + 1) = 2 *(x + 1) = 2 *(x + 2) = 3 *(x + 2) = 3 *(x + 3) = 4 *(x + 3) = 4 *x = 1 *x = 1 offset x = 4 offset x = 4 for (int i = 0; i < size; ++i) { cout << "*(x + " << i << ") = " << *(x + i) << endl; } cout << "*x = " << *x << endl; cout << "offset x = " << reinterpret_cast<int>(x + 1) - reinterpret_cast<int>(x) << endl; return 0;
Background image of page 9

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

View Full DocumentRight Arrow Icon
–10– Multidimensional Arrays Multidimensional Arrays Again, definition is similar to single dimensional array: ± type name[size1][size2]. ..[sizeN] Same operations, only more abstraction: ± x[i][j][k] = *(*(*(x + i) + j) + k) ± *(x + i) ± NOTICE! * is not “contents of” ± * here means take away one type
Background image of page 10
–11– Pointer Math Types Pointer Math Types int x[2][3][5]; Types: ± x : [2] of [3] of [5] of int ± *x : [3] of [5] of int ± **x : [5] of int ± ***x : int (only last * changes the value!)
Background image of page 11

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

View Full DocumentRight Arrow Icon
–12– Pointer Math Sizes Pointer Math Sizes Learn this early! Your success depends on it!
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/09/2008 for the course CIS 450 taught by Professor Neilsen,mitch during the Spring '08 term at Kansas State University.

Page1 / 35

lecture12 - CIS 450 Computer Architecture and Organization...

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

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