Pointers 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 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 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! char pointers in C • For any data type, whether built-in or userdefined, 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 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 • • • • 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) 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! 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); 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) 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 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 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. 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 pointerlike things • References in Java are practically the same as pointers in Pascal, but with a simpler syntax The End