[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 13
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] <13> [14] [15] [16] [17] [18] [19] [20]
Podcasts: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Feb 20 18:41:53 PST 2008

Contents


    pages 157-176 Addresses

      Reading

        Introduction -- Pointers are essential but dangerous in C++

        Pointers are a very important and risky part of the C++ language. They are not intuitive. You have to remember what the machine does with data to be able to use them. Pointers are variables that can store an address. The asterisk operator is used to get the contents of that address. You must never apply it to something that is not an address. Conversely, the ampersand operator gives you the address of a variable. Check out the syntax box on page 162. The 'new' operator gives you new storage and returns it's address. Delete returns the new storage to a heap of recuclable memory.

        5.4.1 Primary Memory

        Think of memory as a large number of numbered boxes each containing one character. Primary memory holds the compiled program and the programs variables and constants. Secondary memory holds files. BUT both are just numbered boxes...

        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
        pik
        &k15

        In Figure 5.2 (page 159)
        pi1pi2k
        &k&k15

        The code on page 160 does the following to memory
        Statementknpipi2
        int k = 15, n;15?
        int *pi, *pi2;15???
        pi=&kl;15?&k1?
        pi2=&n;15?&k1&n
        *pi=*pi2;1515&k1&n

        Uninitialized Pointers -- a very common execution error

        Programs that don't initialize pointers smell and typically suddenly crash after all the testing is done.

        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

      1. &a[0] == a

        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

          Uninitialized pointers

          Lingering pointers

          Repeated allocation

          Garbage -- not in the book

          Garbage is a piece of allocated memory which has no pointer. This means you run out of memory when it is full of recyclable garbage.

        Note -- You do not need to study 5.4.7 5.4.8

      . . . . . . . . . ( end of section Reading) <<Contents | End>>

      Glossary

      1. address::jargon="The number that identifies a location in computer memory".
      2. location::jargon="the smallest piece of addressable memory", all locations have a fixed and unique address and a single character (byte) of data called its contents. The contents can change when a statement executes.
      3. contents::jargon="The character (byte) found in a given location".

      . . . . . . . . . ( end of section Glossary) <<Contents | End>>

      Questions

        What should I use instead of a break statement

        I tend to use Boolean variables plus if-else statements. As a simple example:
         		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.

        How many different functions are there

        There are an infinity of functions.... for more take CSCI546

        Could you show us a function example like the one in the quiz

        Nope!

        What is the Hexadecimal and octal forms in terms of bits

        Hexadecimal notation groups the bits in fours. The octal notation groups them in threes. So the bit pattern
         		001010100111
        is split like this
         		0010 1010 0111
        and 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 111
        giving 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.
      1. absolute = base + relative.

        Please explain the sizeof operator

        First notice the spelling is not English -- there is no space between 'size' and 'of'.

        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 int
        will 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
        1. Going beyond the last index of an array or vector.
        2. No terminating '\0' at the end of a char text.
        3. Not leaving room for a '\0' at the end of a char[].
        4. Following the NULL pointer.
        5. Not initializing a pointer before it is used.
          A loop that moves a pointer into terra incognita.
        6. Assigning an integer value to a pointer : int *p = (int*) 17;

        How do pointers and text strings work together

        A text string is an array of characters that ends with a special null character '\0'. Each element in this array can be refered to by a pointer. Mostly there are some standard for loops that are used. For example see [ strlen page 168 ] and [ strcpy page 169 ] plus [ lab07/ ] for examples.

        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.

        How are pointers used in todays popular programs

        I'd love to.... but most software is clased and a trade secret, so I can only guess.

        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:
        Commandp1p2
        -αβ
        p1=p2
        -ββ

        The command *p1=*p2 changes the place that p1 points to by the place that p2 points to:
        Commandp1p2αβ
        -αβab
        p1=p2
        -αβbb

        To see why this is review what a pointer is and how it works.

        Can you have an array of pointers

        Yes. They are even useful for some programs. For example in CSCI202 we declare the main function as having an array of pointers to texts:
         int main ( int argc, char* argv[] )
        which are the arguments of the prorgam when executed as a command!

        Can you have a vector of pointers

        Yes. For example for any type T
         		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.

        What is a lingering Pointer

        This happens when you use a pointer to allocate memory that has been deleted. The data is no longer there and the program (with luck) crashes.

        How do you use a lingering pointer

        You avoid it like the plague.

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

        Can there be any use in garbage -- allocated memory with no pointer

        We call it garbage because it can't be used any more. It is wasted space. We can't even delete and so recycle it. It is in the computer's land fill: the heap. However when the program finishes all the memory is given back to the operating system.

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

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. OOP::="Object-Oriented Programming", Current paradigm for programming.
  4. SP::="Structured Programming", Previous paradigm for programming.
  5. TBA::="To Be Announced", something I should do.
  6. TBD::="To Be Done", something you have to do.
  7. UML::="Unified Modeling Language", [ 15.html ] (class notes on the UML and OOP).

End