Sept 24 2010 class lecture notes for OOP344

From CDOT Wiki
Jump to: navigation, search

Pointers

  • address
    beginning of location in memory
    just an unsigned integer but adding one to a pointer, adds the size of the target to the pointer
  • sizeof(pointer) is always the same regardless of the type of the targer of the pointer

Declaration of Pointers

  • int* p, not int *p (integer pointer variable named p, not integer variable of pointer named p)
  • undefined pointer points to "garbage"

Int vs. pointer

  • int can be signed (positive / negative)
  • pointers are unsigned (only positive) and adding one to a pointer, adds the size of the target to the pointer

Use of pointers

  • &a = "address of variable a"
  • *p = "where p is pointing to" OR "data at pointer p"
  • int* p = 8; (pointer p is now pointing to memory location 8)
  • if p is an int pointer variable, p++ will increment the address of p by the memory size of an int (so p will be 12)

Arrays

  • 'int a[5];' creates 6 objects.... 5 integers and 1 pointer
  • *a is the same as a[0]
  • string = array of characters terminated by a null character
  • Dereferencing operator (*) takes precendence over +/- operators (Quiz alert)
  • Using 'int a[5] = {10, 20, 30, 40, 50};', *a would point to the beginning of the array, where the value '10' is located
  • '*a+2' first resolves data at a, and then adds 2, giving us '12'
  • '*(a+2)' gives us the data 2 integers 'forward' from the beginning of the array 'a'
  • With two-dimensional arrays, 'a' will point to a 'pack', or 'row', of integers. (a + 1) will move to the next pack, or row, of integers.
  • Multi-dimensional arrays are actually represented as single-dimension arrays within the system memory.

Lazy Evaluation

  • When evaluating numerous condition statements connected with && (AND), the moment a "false" is encountered, no further statements are evaluated, because the statement as a whole is false.
  • A method of substituting if statements by using && (AND) operand.
  • Pseudocode : (boolean statement) && (action performed if boolean statement is true)
  • Lazy Evaluation is generally faster than if statements.

Misc

sizeof(int); = operator that returns the memory size of data type given as argument. in this case, it returns the size of an int. can also operate on objects.

Credits

Notes taken by Dale Karp and Daniel Slessarev