Lecture Notes for Introduction to Computer Science II

31 July 2001 - Introduction to Pointers


  1. memory and addresses

    1. the programs and data a computer manipulates are kept in main store or memory

    2. main store is an array of bytes; fixed length, but large (4 gig)

    3. the array indices are known as addresses

    4. low indicies, low addresses, first address, address

    5. group access to elements

  2. values and memory

    1. multi-byte values are stored in contiguious sections of memory - ints 4 bytes; character arrays in n bytes; objects too

    2. some data layout restrictions - int alignment; another way your code can crash

    3. the sizeof() function returns the number of bytes in a value or type

  3. variables and memory

    1. a variable represents a piece of memory used to hold a value of some type; the value type determines the size of the memory piece

    2. the value held by a variable is actually held by the associated memory piece

    3. a variable is the address of the associated memory piece

    4. examples - assigment, function parametes

  4. pointers and memory

    1. a pointer is an address in memory

    2. a pointer can be used to access the data at the memory address to which it refers

    3. a pointer is like an integer, but is not an integer

    4. a pointer has a type to determine the kind (size of the) of value it points to

    5. a pointer is just another value that can be stored in memory

  5. pointer pros and cons

    1. pros - provides great flexibility; create efficient code; conceptually easy to understand

    2. cons - easy to screw up; creates mysterious errors; difficult to make portable pointer-based code; complex interactions with other c++ features, specially objects.


This page last modified on 1 August 2001.