pointers. the structure of memory computer memory is a linear sequence of addressable locations...

15
Pointers

Upload: cecily-curtis

Post on 02-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Pointers

Page 2: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

The structure of memory

• Computer memory is a linear sequence of addressable locations

• Addresses are numbered starting at zero• In personal computers, the smallest addressable

unit is a byte (8 bits)• In large scientific computers, the unit is a "word"

– A word is typically 32 to 128 bits, or even more

Page 3: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Pointers

• A pointer is an address of a storage location• By convention, zero represents the "null" pointer• A pointer is a number, and must itself be stored• The size of a pointer depends on the amount of

memory to be addressed• A 16-bit pointer can address 64K locations• A 32-bit pointer can address 4 trillion locations

Page 4: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Pointers in higher-level languages

• FORTRAN and Algol had no (user-level) pointers• C makes pointers an arithmetic type• Pascal provides pointers, but restricts their use• Java has pointers but tries to hide them from the

user– ...but it throws a NullPointerException!

Page 5: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

char pointers in C

• For any data type, whether built-in or user-defined, C allows a pointer to that type

• char *pch makes pch a pointer to a character• You can use *pch as a character• You can use pch as a pointer to a character• You can do arithmetic on pointers

• pch++ increments pch by the size of a character

Page 6: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Other pointers in C

• If T is a type, T *p declares p a pointer to that type• You can use p as a pointer to a T• You can use *p as a T• p++ increments p by the size of a T

– Important because of the way arrays are treated• You can make a pointer to any variable

– If x is any variable, then &x is its address

Page 7: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Storage allocation in C

• You can allocate storage (from the "heap")

– To allocate storage, use malloc(num_bytes)– This gives a pointer of unknown type: (void *)

• You then cast the pointer to the desired type

• Example:

– int *myStorage;myStorage = (int *) malloc(100);

– This gives you space for 25 integers (100 bytes)

Page 8: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Arrays in C

• Array indexing is syntactic sugar for pointers

• a[i] is treated as *(a+i)• To zero out an array:

– for (i = 0; i < size; i++) a[i] = 0;– for (i = 0; i < size; i++) *(a+i) = 0;– for (p = a; P < a+size; p++) *p = 0;

• Because a[i] means *(a+i), i[a] is equally legal!

Page 9: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Strings in C

• A character in C is a single (ASCII-encoded) byte• A string is a pointer to a character (a char*)• The first zero character ends the string• You must allocate space for a string

– str = (char *) malloc(101);• To process all the characters of a string:

– for (p = str; *p != 0; p++) process (*p);

Page 10: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Parameter transmission in C

• All parameters are passed by value• To fake pass by reference, pass a pointer• Example: to add 1 to x, call addOne(&x)• Where:

void addOne(int *n) { *n = *n + 1; }• "Real" call by reference does exactly this, but

automatically (so you don't need the *n syntax)

Page 11: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Problems with pointers in C

• No bounds checking--pointers can point outside the array,or even outside the program

• No type checking--you can cast a pointer to anything

• Memory leaks--you can forget to deallocate storage when you're done with it

• Dangling references--you can deallocate storage before you're done with it

• C enthusiasts say you just have to be careful

Page 12: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Pointers in Pascal

• p: ^T; makes p a pointer to type T• To follow the pointer (and get the T), say p^• To allocate space for a T, say new(p)• You can assign pointers to pointer variables• You can test pointers for equality and for null• You can NOT do pointer arithmetic• ...and that's about all

Page 13: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Pascal needs pointers less than C

• Pascal has true arrays• A string is an array of characters

– Strings are clumsy, but they don't use pointers• Pascal has both call by value and call by reference

– No clumsy syntax needed in called routine• Pointers are still adequate for building data

structures such as linked lists, trees, etc.

Page 14: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

Pointers in Java

• Java calls pointers "references"• You cannot directly manipulate references in Java• Any object reference is effectively a pointer• You can allocate memory (with new), use

references, compare references, and do pointer-like things

• References in Java are practically the same as pointers in Pascal, but with a simpler syntax

Page 15: Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,

The End