Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Advertisements

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Pointers OVERVIEW.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamic memory allocation and Pointers Lecture 4.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSCE 210 Data Structures and Algorithms
8 Pointers.
Object Oriented Programming COP3330 / CGS5409
Presentation transcript:

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures

Prof. amr Goneid, AUC2 Pointers & Dynamic Data Structures

Prof. amr Goneid, AUC3 Pointers & Dynamic Data Structures Static Data Structures The Address of a Variable: Pointers Dereferencing Pointers to Arrays Dynamic Data Structures Run-Time 1-D Arrays Run-Time 2-D Arrays

Prof. amr Goneid, AUC4 1. Static Data Structures The usual variables we declare in the program are static (we cannot get rid of them or change their size). For example: int k; float x = 2.15; Arrays and Structs are also static, so once declared, we cannot erase them from memory or change their size. For example: float x [20]; int a[3] = { 2, 2, 4 };

Prof. amr Goneid, AUC5 Static Data Structures Static data are allocated memory at Compile Time, i.e. before the program is executed. Static data are allocated their memory space in a place called the Data Segment Static data cannot change size during Run Time, i.e. while the program is running.

Prof. amr Goneid, AUC6 Where in Memory? The Memory Map: one segment = 64 kbyte DS = Data Segment (Static Data) CS = Code Segment ( Main & Functions code) SS = Stack Segment (System Stack) Heap = Rest of Memory (for Dynamic Data) OS = Operating System Area OSCSDSSSHEAP LM HM

Prof. amr Goneid, AUC7 2. The Address of a Variable: Pointers The & symbol is called the address operator The purpose of & is to return the address of a variable in memory. For example, if x is a variable then &x is its address in memory. We can store the address of a variable in a special variable called a pointer A Pointer is a variable whose value is a memory address of an item, not its value A pointer knows about the type of the item it points to All pointers have fixed size (typically 4 bytes)

Prof. amr Goneid, AUC8 Pointers A pointer variable must be declared before it is used. It must be bound to the same type as the variable it will point to. The asterisk operator * must precede each pointer name in the declaration variableName; * pointerName; ………………………………. pointerName = & variableName; Same Type

Prof. amr Goneid, AUC9 Pointers Q: Why is it important to declare the type of the variable that a pointer points to? A: For an operation like “p++” where “p” is a pointer variable, the compiler needs to know the data type of the variable “p” points to. If “p” is a character pointer then “p++” will increment “p” by one byte, if “p” were an integer pointer its value on “p++” would be incremented by 2 bytes.

Prof. amr Goneid, AUC10 Pointers For Example: double x = 3.14; // a variable of type double double *p;// a pointer to double p = &x;// p now stores the address of x 3.14 x p 0012FF78 Starts at location 0012FF78

Prof. amr Goneid, AUC11 Pointers Pointers can only contain addresses. e.g. if we declare: float *p, *q ; then the following are errors: p = 2.55; q = 15.5;

Prof. amr Goneid, AUC12 Pointer Arithmetic The ++ and -- operators may be used to increment or decrement a pointer variable. An integer may be added to or subtracted from a pointer variable. A pointer may be subtracted from another pointer.

Prof. amr Goneid, AUC13 3. Dereferencing (Indirection) (*) is the dereferencing (or indirection) operator. It can be used to access the value stored in a location. For example, if (p) is a pointer, the value of (*p) is not the address stored in p but is instead the value stored in memory at that address (i.e. 3.14)

Prof. amr Goneid, AUC14 Indirection Operator An asterisk has two uses with regard to pointers  In a definition, it indicates that the object is a pointer char *s; // s is of type pointer to char  In expressions, when applied to a pointer it evaluates to the object to which the pointer points (indirection or dereferencing) int k = 1; int *p = &k; // p points to k *p = 2; cout << k << endl; // display a 2

Prof. amr Goneid, AUC15 Example Program int main() { double x = 3.14; double *p; int k = 5;int *q; p = &x;q = &k; cout << "Address of x is " << p << " Value of x = " << *p << endl; cout << "Address of k is " << q << " Value of k = " << *q << endl; return 0; } Output: Address of x is 0012FF78 Value of x = 3.14 Address of k is 0012FF70 Value of k = 5

Prof. amr Goneid, AUC16 Pointers as function parameters: Swap Function void Pswap ( int *p, int *q) { int temp = *p; *p = *q; *q = temp; } int main ( ) { int a = 5; int b = 7; Pswap (& a, & b); cout << a << ‘ ‘ << b << ‘ ‘; return 0; }

Prof. amr Goneid, AUC17 Pointer to a Pointer A pointer can be made to point to another pointer. Example: int c = 23; q p c int *p; int **q; p = &a; q = &p; cout << a << ‘ ‘ << *p << ‘ ‘ << **q << ‘ ‘; Output:

Prof. amr Goneid, AUC18 4. Pointers to Arrays C++ regards the name of the array as the address (pointer) of the first element in the array Example: int a[3] = { 12, 55, 93 };int *r = a; cout << *r ; //equivalent to a[0], gives 12 cout << *r+1;// equivalent to a[0]+1, gives 13 cout << *(r+1); // equivalent to a[1], gives 55 cout << *(r+2) + 7; // equivalent to a[2]+7, gives 100 cout << *(++r);// now r points to a[1], gives 55 r--;// brings back r to point to a[0]

Prof. amr Goneid, AUC19 Pointers to Arrays // This program uses a pointer to display the // contents of an integer array. #include void main(void) { int set[8] = {5, 10, 15, 20, 25, 30, 35, 40}; int *nums, index; nums = set; cout << "The numbers in set are:\n"; for (index = 0; index < 8; index++) { cout << *nums << " "; nums++; }

Prof. amr Goneid, AUC20 5. Dynamic Data Structures The Heap ( free memory) A Dynamic Data Structure is allocated memory at run-time. Consists of nodes to store data and pointers to these nodes to access the data. Nodes are created (allocated) and destroyed (de- allocated) at run-time. Using dynamic allocation allows your programs to create data structures with sizes that can be defined while the program is running and to expand the sizes when needed.

Prof. amr Goneid, AUC21 Nodes & Pointers A node is an anonymous variable (has no name) No name is needed because there is always a pointer pointing to the node. Heap Node Pointer

Prof. amr Goneid, AUC22 Creating Nodes: the “new” Operator The new operator allocates memory from the heap to a node of specified type at Run Time. It returns the address of that node. The statements: int *p ; ……………………………………. p = new int; create a new node of type int and let a pointer p point to it. No data is put into the node The node created has no name, it is called an Anonymous Variabe. It can only be accessed via its pointer using the indirection operator, i.e. by using (*p)

Prof. amr Goneid, AUC23 Accessing Data with Pointers * - indirection operator *p = 15.5; // *p reads as: contents of node pointed to by p Stores floating value 15.5 in the node pointed to by p 15.5 p *p

Prof. amr Goneid, AUC24 Example: float *p; p = new float; *p = 15.5; cout << “The contents of the node pointed to by p is “ << *p << endl; Output The contents of the node pointed to by p is 15.5

Prof. amr Goneid, AUC25 Pointer Operations Assignment: Only if the two pointers are bound to the same type *p = 3.3; *q = 1.5; q = p ; Comparison: only when both are bound to the same type Only equality or inequality, e.g. p == q or p != q if ( p == q ) … q p p q lost BeforeAfter 1.5

Prof. amr Goneid, AUC26 Pointer Operations Copy contents of one node into another node *p = 3.3;*q = 1.5; *q = *p ; The Null Pointer: p = NULL; // Now p points to nothing e.g. if ( p == NULL ) …. 3.3 q p p q BeforeAfter 1.5 p

Prof. amr Goneid, AUC27 Returning Nodes to the Heap Operation: Returns space of node pointed to by pointer back to heap for re-use When finished with a node delete it Pointer is not destroyed but undefined : Example: delete ; delete p;

Prof. amr Goneid, AUC28 6. Run-Time 1-D Arrays Drawbacks of static arrays:  Capacity is fixed at compile time  If size > number of elements, memory is wasted  If size < number of elements, we suffer array overflow Solution: Dynamic (Run-Time) Arrays:  Capacity specified during program execution.  Acquire additional memory as needed.  Release memory locations when they are not needed.

Prof. amr Goneid, AUC29 Run-Time 1-D Arrays The operator new can be used in an expression of the form: n is an integer expression (could be a variable). This allocates a 1-D array with n elements, each of type ; it returns the base address of that array. The address returned by new must be assigned to a pointer of type Type. new [n] n-1

Prof. amr Goneid, AUC30 Example int n; cout << “Enter size of array: "; cin >> n;// size is entered at run-time if (n > 0) { int *A = new int [n]; // A is now the base address // process A for (int i = 0; i > A[i];... }

Prof. amr Goneid, AUC31 Run-Time 1-D Arrays Because run-time arrays can take a lot of memory from the heap, we must de-allocate that space after we finish with it. To return memory allocated to array pointed to by A, use the delete operator in the form: delete [ ] A;

Prof. amr Goneid, AUC32 Example int n; cout << “Enter size of array: "; cin >> n;// size is entered at run-time if (n > 0) { int *A = new int [n]; // A is now the base address // process A for (int i = 0; i > A[i]; ……….. delete [ ] A; // Release memory locations }

Prof. amr Goneid, AUC33 7. Run-Time 2-D Arrays  Run-Time arrays are always 1-D arrays with a pointer giving its base address.  Physically, 2-D arrays are 1-D arrays of arrays.  We can work with a “virtual” 2-D array that is physically 1-D array by “mapping” elements at a given row and column to the 1-D array

Prof. amr Goneid, AUC34 Run-Time 2-D Arrays  Suppose we want to process a 2-D dynamic array of integers with N rows and M columns (size is N*M).  Dynamic allocation: int *B = new int[N*M];  The element at row (i) and column (j) is physically the element B[k] where k = j + i * M; with (i = 0…N-1 and j = 0…M-1)

Prof. amr Goneid, AUC35 Run-Time 2-D Arrays  Example: a 3x4 array N = 3; M = 4; int *B = new int[N*M]; for (i = 0; i < N; i++) for (j = 0; j < M; j++) { k = j + i * M; cout << k <<“ “;  Output is the index in the physical 1-D array: Virtual Row numberVirtual Col number Physical index No. of Columns

Prof. amr Goneid, AUC36 Run-Time 2-D Arrays  Inverse mapping: To convert from an index (k) in the physical 1-D array to a row (i) and a column (j) in the virtual 2-D array: k = 0,1,….., N*M-1 i = k / M; ( a number 0…N-1) j = k % M; ( a number 0…M-1) For example with N = 3 and M = 4, k = 6 gives i = 1 and j = 2

Prof. amr Goneid, AUC37 Physical Run-Time 2-D Arrays It is possible to create PHYSICAL 2-D run-time arrays using an array of pointers to 1-d arrays. The following function receives the number of rows N and the number of columns M and returns a pointer to a 2-D array of integers of size N x M int ** Matrix (int N, int M) { int ** m; m = new int * [N]; for (int i = 0; i < N; i++) m[i] = new int [M]; return m; }

Prof. amr Goneid, AUC38 Physical Run-Time 2-D Arrays The function can now be used in a main program as follows: int main ( ) { int N = 20; int M = 30; int ** B; B = Matrix (N, M); // B can now be used as a 2-D array // B[i][j] is the element at row i and column j

Prof. amr Goneid, AUC39 Physical Run-Time 2-D Arrays To delete this array, each row must be deleted individually before deleting the B pointer: for (int i = 0; i < N; i++) delete [ ] B[i]; // deleting row i delete [ ] B;