The following are answers to questions that you can answer prior to the lab. These questions and answers are specific to C++. Pointers and memory management will be important to your success in CS210. Please consider thoroughly reviewing pointers and dynamic memory if you had trouble answering these questions.
A pointer is a special variable that refers to a memory address. Pointers generally match the type of data that they refer to. There are some exceptions to this. A pointer may refer to a parent class, but point to a child of that class. This is called polymorphism. In C and C++ a void pointer may point to any type of memory, but to use it you must cast it to a typed pointer.
pOpponent = new AI_Opponent;
(or pOpponent = new AI_Opponent();. Both are valid, but the latter is dangerous because you might think AI_Opponent opponent(); would work. It doesn't. It is a prototype for a function called opponent that returns an AI_Opponent object.
(or (*pOpponent).draw();, but this is not typical.
There are three ways to do this:
Such a pointer is called a null pointer. It is guaranteed never to be equal to an address generated with new.
The program should crash. On a UNIX command line you would get a "Segmentation fault" message. In Visual Studio if you run without debugging it will just say the program stopped working. If you run with debugging in Visual Studio, it will tell you there was an "Access violation"
If you are lucky, the program will crash right away with a segmentation fault or access violation. If you are unlucky, the operating system might not notice that the memory doesn't belong to the pointer and you might get an unexpected color.
If you are certain the thing is no longer needed anywhere in the program, delete it. Then, either ensure the pointer refers to a new valid location, or make it a null pointer.
You use delete.
|CS Dept Home Page||CS Dept Class Files||CS210 Class Files|
© 2015 Department of Computer Science, University of Regina.