.Open CSci202 Computer Science II, Session 04, Objects and the UML (Previous): Review data types and pointers .See http://www/dick/cs202/03.html . Project 1 due at start of class . Preparation Study this page and Chapter 8 on Pointers and write down the questions, .List 8 Pointers and Pointer-Based Strings 408 8.1 Introduction 409 8.2 Pointer Variable Declarations and Initialization 410 8.3 Pointer Operators 411 8.4 Passing Arguments to Functions by Reference with Pointers 414 8.5 Using const with Pointers 418 8.6 Selection Sort Using Pass-by-Reference 425 8.7 sizeof Operator 428 8.8 Pointer Expressions and Pointer Arithmetic 431 8.9 Relationship Between Pointers and Arrays 434 8.10 Arrays of Pointers 438 8.11 Case Study: Card Shuffling and Dealing Simulation 439 8.12 Function Pointers 445 8.13 Introduction to Pointer-Based String Processing 450 .Set 8.13.1 Fundamentals of Characters and Pointer-Based Strings 451 8.13.2 String-Manipulation Functions of the String-Handling Library 453 .Close.Set 8.14 Wrap-Up .Close.List . 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: .See http://c2.com/cgi/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 .As_is #include 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 .Key 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: .As_is Type * pointer_to_a = a+number; where `a` is declared like this .As_is 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? .As_is int **bucket= new int*[9]; .As_is for (int i = 0;i<=9;i++) .As_is bucket[i] = new int[size]; .As_is for (int i = 0;i<9;i++) //cleanup .As_is delete bucket[i]; .As_is 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 .See http://www/dick/cs202/lab02.html .Close CSci202 Computer Science II, Session 04, Objects and the UML (Next): .See http://www/dick/cs202/05.html