1 / 15

Pointers

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"

giona
Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers

  2. 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

  3. 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

  4. 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!

  5. 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

  6. 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

  7. 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)

  8. 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!

  9. 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);

  10. 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)

  11. 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

  12. 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

  13. 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.

  14. 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

  15. The End

More Related