[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci202] / 04
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] <04> [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Apr 15 10:04:07 PDT 2009

Contents


    CSci202 Computer Science II, Session 04, Objects and the UML


      (Previous): Review data types and pointers [ 03.html ]

      Project 1 due at start of class

      Preparation

      Study this page and Chapter 8 on Pointers and write down the questions,
      1. 8 Pointers and Pointer-Based Strings 408
      2. 8.1 Introduction 409
      3. 8.2 Pointer Variable Declarations and Initialization 410
      4. 8.3 Pointer Operators 411
      5. 8.4 Passing Arguments to Functions by Reference with Pointers 414
      6. 8.5 Using const with Pointers 418
      7. 8.6 Selection Sort Using Pass-by-Reference 425
      8. 8.7 sizeof Operator 428
      9. 8.8 Pointer Expressions and Pointer Arithmetic 431
      10. 8.9 Relationship Between Pointers and Arrays 434
      11. 8.10 Arrays of Pointers 438
      12. 8.11 Case Study: Card Shuffling and Dealing Simulation 439
      13. 8.12 Function Pointers 445
      14. 8.13 Introduction to Pointer-Based String Processing 450
        • 8.13.1 Fundamentals of Characters and Pointer-Based Strings 451
        • 8.13.2 String-Manipulation Functions of the String-Handling Library 453
      15. 8.14 Wrap-Up

      Assigned Work Due

      A Question on the above. Before 10am!

      Chapter 8 pages -- pointers

      why do we use pointers?

      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

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  3. Data_Structure::=A small data base.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. KDE::="Kommon Desktop Environment".
  7. object::="A little bit of knowledge -- some data and some know how", and instance of a class".
  8. OOP::="Object-Oriented Programming", Current paradigm for programming.
  9. Semantics::=Rules determining the meaning of correct statements in a language.
  10. SP::="Structured Programming", a previous paradigm for programming.
  11. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  12. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  13. Q::software="A program I wrote to make software easier to develop",
  14. TBA::="To Be Announced", something I should do.
  15. TBD::="To Be Done", something you have to do.
  16. UML::="Unified Modeling Language".
  17. void::C++Keyword="Indicates a function that has no return".

End