Lecture Notes for Introduction to Computer Science II
31 July 2001 - Introduction to Pointers
- memory and addresses
- the programs and data a computer manipulates are kept in main store or
memory
- main store is an array of bytes; fixed length, but large (4 gig)
- the array indices are known as addresses
- low indicies, low addresses, first address, address
- group access to elements
- values and memory
- multi-byte values are stored in contiguious sections of memory - ints 4
bytes; character arrays in n bytes; objects too
- some data layout restrictions - int alignment; another way your code
can crash
- the
sizeof()
function returns the number of bytes in a value or
type
- variables and memory
- 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
- the value held by a variable is actually held by the associated memory
piece
- a variable is the address of the associated memory piece
- examples - assigment, function parametes
- pointers and memory
- a pointer is an address in memory
- a pointer can be used to access the data at the memory address to which
it refers
- a pointer is like an integer, but is not an integer
- a pointer has a type to determine the kind (size of the) of value it
points to
- a pointer is just another value that can be stored in memory
- pointer pros and cons
- pros - provides great flexibility; create efficient code; conceptually
easy to understand
- 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.