Each box is called a location. It has a number stuck on it.
The number on the box is called its address. It is an unsigned int.
Each location contains ONE character. It is called the contents of the location.
A variable is given a fixed set of adjacent locations when it is declared. The number of locations given to variable v is written sizeof(v). (MEMORIZE)
Assignment to a variable changes the contents of the locations given to that variable.
The address of the first location of variable v is written &v. (MEMORIZE)
5.4.2 Pointer variables
A pointer is a variable that has an address as it's value.
A pointer is a set of locations that are the address of a location in memory.
If p is a pointer then it has an address and it contains an address! We say it "points at the other address."
To find the the contents of the location that p "points at" write * p. (MEMORIZE)
Tracing pointers
The best way to figure out a program with a pointer in is to
tabulate the values of the pointers. Use the "&" symbol.
For example in figure 5.1 (top page 159) the memory looks like this
| pi | k |
|---|---|
| &k | 15 |
In Figure 5.2 (page 159)
| pi1 | pi2 | k |
|---|---|---|
| &k | &k | 15 |
The code on page 160 does the following to memory
| Statement | k | n | pi | pi2 |
|---|---|---|---|---|
| int k = 15, n; | 15 | ? | ||
| int *pi, *pi2; | 15 | ? | ? | ? |
| pi=&kl; | 15 | ? | &k1 | ? |
| pi2=&n; | 15 | ? | &k1 | &n |
| *pi=*pi2; | 15 | 15 | &k1 | &n |
Hairy declarations -- pointers to vectors and constants
5.4.3 Pointers and Arrays
An array is a variable -- a name bound to a set of adjacent locations.
And the symbolic name for an array is replaced by the address of the first location. So
page 165 -- sum ( int *, int )
Summary of pointers and arrays is on page 166.
Pointer arithmetic -- high powered magic
If you add a number to a pointer to a variable v then the
pointers moves by that number times sizeof(v).
This makes arrays work!
This lets us scan across an array very easily. Page 165 for example.
5.4.4 Pointers and text strings
The fastest code for handling text strings uses pointers. The two examples
date back to the first C programming language manual. I don't expect you
to memorize these. But they do show a well known way of coding.
strlen page 168
strcpy page 169
5.4.5 Memory Allocation -- new and delete
Getting more memory as the program runs..... but how do you find it
again. Answer: put the address in a pointer.
Note: you also need to throw the data away when you've used it with the delete statement.
Lets skip this topic until CS202.
How to trace Pointers to allocated memory
The only way I know for working out programs that have pointers
to allocated memory in them
is to invent pretend addresses for the locations they point at! I
often use '1234' and I've also used Greek letters α, β, γ,
and so on....
Example -- CSCI202
5.4.6 Common Errors with pointers -- vital information here
. . . . . . . . . ( end of section Reading) <<Contents | End>>
Glossary
. . . . . . . . . ( end of section Glossary) <<Contents | End>>
Questions
for (i=0; i<N, i++)
{
A;
if(C) break;
B;
}We can vanish the break like this:
bool unbroken=true;
for (i=0; i<N and unbroken, i++)
{
A;
if(C) unbroken = true;
else
{
B;
}
}But -- if you have a working program with one or two breaks you might as well leave it alone.
001010100111is split like this
0010 1010 0111and each 4-bit nibble is turned into a hex digit, giving the equivalnet hex code: 2A7.
In octal the same bit pattern is split like this
001 010 100 111giving the octal form: 1247.
Octal and Hex are popular because these translations are simple to do
(compared to decimal vs binary).
What is the difference between absolute and relative addresses
The relative addresses start counting at some base. Absolute start
counting with address 0.
Next, you can put 'sizeof' in front of any variable and the compiler will calculate the storage that it will give to it and replace the expression 'sizeof v' by the number of bytes allocated to 'v' -- by the compiler. This does not include any storage allocated while the program runs with 'new'.
You can also use it with the name of a data type
sizeof intwill tell you the number of bytes used in one int.
Why are there different forms
Arabic notation with 10 digits is based on humans having 10 fingures.
Binary is because electronic circuits that have 2 values are
fast, cheap, and accurate. Octal and Hex come from the need to write down
and remember complex binary numbers simply.
I don't know why have both Octal and Hex. But it is the older
computer scientists who think in Octal...
Is there a reason to use double instead of float
Double is more accurate.
Review Pass by reference and pass by constant reference
Both share access to data without copying it. Pass be reference lets
a function access and change the data given to it (as an address).
But with a constant reference the function can not change it.
With pass by value ... changes are made to the function's copy of the
given value and do not propagate back to the calling program.
What does a segmentation fault mean
It means a pointer has gone wrong. Here are some common causes
There are more complex algorithms. I think of pointers as little fingers pointing to places in the array of chars. The ++ operation and the -- operation move the fingers in opposite directions.
For example you can have two pointers starting at opposite ends and use them to test to see if a text string is a palindrome
bool palindrome(char* a)
{
bool palindrome=true;
char *p = a;
char *q = a+strlen(a)-1;
for( ; p < q and palindrome; p++, q--)
if( *p != *q )
palindrome=false;
return palindrome;
}You can download [ 13palindrome.cpp ] (function + tests) and see if it works.
I have some open source software that I maintain
[ http://www.csci.csusb.edu/dick/cs320/lisp/src/ ]
is an example.... Meanwhile
How are pointers used in practical situations
(1) Parameters passed by reference uses a kind of pointer.
(2) Getting more storage as the program runs. Vectors have an internal pointer
that is points at the heap.
(3) Advanced data structures (CS202 and 330) are all built by using links. The links
are pointers.
(4) All work at the hardware level (drivers) use pointers.
(5) ...
What is different between p1=p2 and *p1=*p2
Assuming that both p1 and p2 are pointers to locations
then
p1=p2;will change p1 to point to the same place that p2 points. Here is a picture of memory before and after:
| Command | p1 | p2 |
|---|---|---|
| - | α | β |
| p1=p2 | ||
| - | β | β |
The command *p1=*p2 changes the place that p1 points to by the place that p2 points to:
| Command | p1 | p2 | α | β |
|---|---|---|---|---|
| - | α | β | a | b |
| p1=p2 | ||||
| - | α | β | b | b
To see why this is review what a pointer is and how it works.
int main ( int argc, char* argv[] )which are the arguments of the prorgam when executed as a command!
vector < T * > vpt;declares a variable vpt which is a vector of pointers to data of type T. Thes are often used in advance object-oriented code.
Some years ago I was working on a complex interpreter for the language prolog for use in CSCI320 (Programming Language). It had a very strange bug that would some times pop up and crash the system -- in one laboratory but not the other lab! I thought it was the print routine, but I couldn't see anything wrong in it. Then I noticed that the crash only occurred when a variable appeared twice in an expression -- in one lab only -- the lab that used some IBM UNIX workstations. And I remembered that IBM computers tend to remove primary from a program when you deallocate it, but other manufacturers let you keep the storage in case you wanted to reuse it. Suddenly it dawned: When one variable appeared in two places the garbage collection routine was deleting the storage twice. The first time was ok but left a lingering pointer. The second time the IBM computers crashed the program. It took about 20 minutes of programming to fix the deletion routine to work properly.....
|
. . . . . . . . . ( end of section Questions) <<Contents | End>>
Syntax
Quiz 6 -- on functions
The tricky stuff -- pass be reference vs pass by value. Also some loops.
Lab 07 -- Fun with Pointers and sizeof
Project 5 -- Resubmit improved P4 on vectors and arrays