Good question.
(1) we are working with code that has pointers already. For example -- we have to use a function that has a pointer as parameter.
(2) they give us a neat solution to a problem -- for example avoiding copying data.
(3) Thats what the question in the quiz or final asks you to do.
(4) You like the challenge: [ wiki?ThreeStarProgrammer ]
In practice I have two rules: (1) Don't, (2) hide the pointer inside an object.
Chapter 8 pages 402 -- pointers
what is "passbyreference" and what does it do?
THis is a way for a function call to pass data to a function parameter. THe actual parameter
in the call must be an address. The function parameter must be specified as
an address. The operations in the function manipulate the location
whose adress they have been given.
Chapter 8 pages 411 -- Null pointer
Is there a difference between being initialized to 0 or null?
On some wierd computers in the past NULL (must be spelled in capitals) was not a 0.
It is smart to use NULL rather than 0 just to make it very clear what you are thinking.
Null is in a useful library
#include <cstdlib>that I think belongs in all real programs!
Chapter 8 pages 411-414 -- Pointer Operators
What are pointer operators used for and how do they work?
The main pointer operator is dereference and is symbolized as an asterisk/star "*" before the pointer. It follows the pointer and returns the value at the end.
The other operators move pointers by using pointer arithmetic: ++, --, +=, -=, ...
There is no multiplication or division of pointers.
However you can take the difference of two pointers to places in the same array.
Chapter 8 pages 412 -- Pointers and Hacking
Do hackers make use of pointers to access system memory for other programs (arrays, etc.)?
Yes. Modern chips tend use segmentation to force programs to crash securely however. Older systems were very open to this.
Chapter 8 pages 414 -- pointer operators
Can you explain how to pass by reference with pointer arguments?
I will translate a simple function from using the C++ "&" reference parameter into a C-style "*" pointer.
Demo....
Chapter 8 pages 415-416 -- Cube by Reference
What exactly does cube by reference do and when would you need to use it?
It is given the address of a variable. It takes that value of the vriable and replaces it by the cube of the original value.
Note this is a classroom example -- you need a better reason to so it in practice -- for example to avoid copying a lot of data in an array or object.
Chapter 8 pages 419 -- Nonconstant Pointer to Nonconstant Data
At the beginning of the paragraph about pointers, it is written: "The highest access is granted by a nonconstant pointer to nonconstant data". What is meant by "highest access"? Can you give an example where a pointer would go before another?
It is not a matter of priority.... but of power and risk. A noncanstant pointer can be moved to access different data. If it also points at nonconstant data then you can change the data. So you have to power to go to ANY place in memory, and do ANYTHING you like to it. They call this "high access" in the book.
It is all about giving parts of your code the privileges they need but stopping them doing the things you don't want.
Chapter 8 pages 418-419 -- Using const with Pointers
What is the benefit of using const with pointers?
See below.
Chapter 8 pages 419-420 -- using const with pointers
I am still confused with the differences and similarities with the nonconstant pointer to nonconstant data, as well as the the nonconstant pointer to constant data...Can you re-explain these terms?
The 'const' qualifier can protect the data that a pointer points at. It can be read but not changed. It can also stop the pointer being moved. This depends on the syntax -- that I find confusing.
Chapter 8 pages 435-438 -- Pointers and Arrays
What are some of the differences and similarities between arrays and pointers.
This is in the book -- exercise for the class.
Chapter 8 pages 438 -- Arrays of Pointers
How to declare a pointer to an array?
You can only declare a pointer and attach it to a particular item in the array:
Type * pointer_to_a = a+number;where a is declared like this
Type a[NUMBER]....;
Chapter 8 pages 8.10 -- Arrays of pointers
In my project I attempted to delete an array of pointers but I received a heap corruption error. I used the below code to create and then delete the array. The error happens after executing the last line of the code. How do I properly dispose of an array of pointers?
int **bucket= new int*[9];
for (int i = 0;i<=9;i++)
bucket[i] = new int[size];
for (int i = 0;i<9;i++) //cleanup
delete bucket[i];
delete []bucket;
I suspect the problem comes from creating 10 rather than 9 buckets in the second line above.
There is also a subtle mismatch between the first line and the last line.
Note this is the kind of code I will do anything to avoid.
Exercises
Quiz 1 -- on chapter 1-7 and Project 1
Lab -- Drawing UML Diagrams
[ lab02.html ]
. . . . . . . . . ( end of section CSci202 Computer Science II, Session 04, Objects and the UML) <<Contents | End>>
(Next):
[ 05.html ]
Abbreviations