CPS 100, Fall 2002 Lecture
Pointers - Aug. 29, 2002


Top Ten List for Surviving in CPS 100

  1. Start assignments early

  2. Seek help when stuck - 1 Hour Rule

  3. Read the newsgroup

  4. Set file permissions to rodger:cps100

  5. Visit Prof. Rodger in her office

  6. Read the book

  7. Keep working until it is correct

  8. Ask Questions

  9. Learn how to spell RODGER

  10. Keep Poppa John's number handy









    More Survival Tips

    • See setup info

      • setting up a cps100 directory

      • setting up permissions SO WE CAN HELP YOU

    • Use the debugger: gdb (text) OR ddd (visual)

      If compiled program is called readwords, and data file is called simple type:

      gdb readwords run simple OR ddd readwords
      • It is your friend!










    What's a Pointer, why good, why bad?

    • Pointer is a memory address

      an indirect reference to memory or an object

      • x is an int, p is a pointer to an int

        int x; int * p;

        • Same thing works with Date, Dice, Students

        • Not much use to have pointers to int except in C to understand arrays

        • pointers to objects are very useful

    • Pointers force us to think about the machine and memory

      • Knowledge is powerful, but freedom from it is liberating

    • Pointers allow us to work at a lower level, but permit inheritance and a higher level of design/programming

      • built-in array and tvector, C-style string and










    Pointers, Memory, Abstractions

    • A pointer is a memory address

      • Addresses like 1, 2, 3, ... 0x0024ab03

        • Hexadecimal or base-16 digit represents 4 bits

        • Character is 8 bits, integer is 32 bits, double 64 bits

      • every variable is stored somewhere in memory

        typically we can ignore where

        double x = 32.6; int y = 18; string s = "hello";

      • string variable s actually the size of an int

        characters are stored elsewhere











      Pointers, Heap, Copies

      • Memory allocated statically (auto) vs dynamically (heap)

      • Example: Date ides(3, 15, 2002); Date * foolptr = new Date(4,1,2002); Date * x = foolptr; Date y = ides;

      • Objects is copied (y above)

      • Pointers are copied, object is not (x above)










      Pointer basics and terminology

      • new, dereference, selector operator, copy semantics

        CD c1("Beatles", "Rubber Soul", 1965); CD c2("Nirvana", "Nevermind", 1991); CD * c3 = new CD("REM", "Reveal", 2001); CD * c4; // what is value of c4? CD c5; // what is value of c5? cout << c1.title() << endl; cout << c3->title() << endl; cout << (*c3).title() << endl; c5 = c2; c2.changeTitle("Incesticide"); cout << c5.title() << endl; c4 = c3; c3->changeTitle("Out of Time"); cout << c4->title() >> endl;

        See animated example.











      What's the Point? (e.g. sharing)

      • Difference between vector of Dates and a vector of pointers to Dates?

        tvector <Date> tv(1000); tvector <Date *> tvp(1000);

        • Which takes up more space?

          What are the values in vectors?

        • What happens when we write:

          tv[0] = tv[2]; tvp[0] = tvp[3];

      • Consider example of sorting by both name and age

        • Should we have two vectors of students?

        • Should we have two vectors of student pointers?

        • Is there a reason to prefer one to the other?










        The trouble with pointers

        • Don't use the address-of operator & Dice * makeDie(int sides) // YES { return new Dice(sides); } Dice * makeDie(int sides) // NO { Dice d(sides); return &d; }

          • What about the code below with different versions?

            Dice * cube = makeDie(4); cout << cube->NumSides() << endl;

        • Pointer Advice

          • Always initialize pointers

            NULL or new

            • NULL means errors are reproduceable

            • Possible to assign another pointer to

          • Never use the addr-of-operator

          • Don't call new unless you want another object!










          Constructors/Destructors

          • Every object created must be constructed

            • if no constructor provided, one will be provided for you

          • Constructors initialize state and allocate resources

            • Resources can be dynamic objects, files, etc.

          • Objects are (or should be) destructed when they're no longer accessible or used

            • For static variables

              happens when they go out of scope

            • For heap-allocated variables

              happens when delete is called on the pointer to an object

              Student * s = new Student("Joe"); delete s; // return storage to heap

            • When object is destructed, the destructor function is called automatically.

              Student::~Student() {...}

          • It is easy to mess up when you are deleting

            • Can't delete the same object twice

            • Can't delete object not allocated by new

            • Never calling delete










            Example Program: clublist.cpp




















          Susan H. Rodger
          Last modified: Mon Sep 2 15:17:58 EDT 2002