1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Chapter 6 Data Types
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Pointers. 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.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CMSC 202 Lesson 13 Pointers & Dynamic Memory. Warmup Overload the subtraction operator on two Money objects as a FRIEND! class Money { public: _______________.
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.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
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:
C++ Pointers Read Chapter 2 (P ). COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :
POINTERS.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
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.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Assembly - Arrays תרגול 7 מערכים.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Engineering Computing I Chapter 5 Pointers and 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.
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers and Arrays Dynamic Variables and Arrays.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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.
Intro to Pointers in C CSSE 332 Operating Systems
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 10: Pointers 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Programming in C Pointers and Arrays.
Presentation transcript:

1 CMSC 202 Pointers Dynamic Memory Allocation

2 A simple variable A variable is drawn as a labeled box int x; X :

3 Complex Variables A complex variable is drawn as complex boxes (horizontal for array, vertical for struct) struct Astruct { int y; char c; }; int intArray [ 4 ]; Astruct myStruct; intArray: myStruct :

4 Pointe value A pointer value is drawn as an arrow. The arrow points to a picture of the object to which the pointer points. The pointer is said to reference the object. 3

5 Pointer Variable A pointer variable is drawn as a labeled box, like any other simple variable int *iPtr; NULL is a pointer value that doesn’t point to anything and is drawn as a box with a slash iPtr:

6 When a pointer variable points to an object … … the base of the arrow representing the pointer value is placed in the pointer variable’s box 42 iPtr :

7 Assignment to pointer variables A pointer variable receives a value through assignment. –Newly allocated space (using new) –Previously allocated space

8 Allocating space with “new” Graphically depict the result of allocating memory with new by 1.Figure out what kind of space is being allocated 2.Draw a box to represent the allocated space 3.Draw an arrow pointing to the new box and place it’s base in the box representing the pointer variable being assigned to

9 A “new” picture struct STRUCT2{ int a; int b; }; STRUCT2 *struct2Ptr; struct2Ptr = new STRUCT2; Struct2Ptr

10 Pointer Assignment Suppose ‘thing1’ and ‘thing2’ are pointers to int and ‘thing1’ references an int whose value is thing1: thing2:

11 Pointer Assignment (cont’d) And now execute the statement thing2 = thing1; 1.Draw a new arrow that points to the box being referenced by the pointer value on the right hand side 2.Place the the base of the new arrow in the box representing the pointer variable on the left hand side 17 thing1: thing2:

12 Dereferencing pointers Given the following picture 17 thing1: The * (star) operator “deferences” a pointer variable. In terms of our picture it means “follow the arrow”. We can use dereferencing to change what a pointer value points to: *thing1 = 42; results in this picture 42 thing1:

13 An exercise for the student Given the following declarations and code typedef int* intPtrArray [ 3 ]; typedef intPtrArray* arrayPtr; arrayPtr cat; int *dog; dog = new int; *dog = 42; cat = new intPtrArray; cat[0] = dog; cat[1] = NULL; cat[2] = new int; *(cat[2]) = 17;

14 The Questions 1.What type of variable is dog? 2.What type of variable is cat? (NO, the answer is NOT arrayPtr). 3.Draw the picture that results from the code

15 The & operator The & (address of) operator is used to create an arrow that points to an existing box. Consider the following code int value; int *pValue; value = 17; pValue = &value; 17 value: pValue: The expression &value gives us a pointer to value

16 Pointers and Arrays A strong relationship between pointers and arrays. –The name of an array is defined to be a pointer to the the first (0 th ) element of the array Consider the declaration int bongo [ 4 ]; The name bongo can be used as a pointer to reference bongo[0]. *bongo = 42; is equivalent to bongo[0] = 42;

17 Pointers to array elements foo: bar: foo + 1 If a pointer value references a particular array element, then adding an integer, n, to that pointer value will result in a new pointer value that references the array element n elements way from the original element

18 Putting these together… … we observe that the nth element of an array called myArray can be referenced as myArray + n Thus, the following expressions are equivalent myArray [ n ] and *(myArray + n) &myArray [ n ] and myArray + n

19 Using ‘delete’ delete is the operator which is used to recycle memory allocated with new The forms of new and delete must match // for a single object int * x = new int; delete x; // for an array of object int *x = new int [ 7 ]; delete [ ] x;

20 A delete example Suppose ‘soup’ is a pointer variable that references an int containing 42 soup: 42 Then the statement delete soup recycles the space referenced by soup. soup: 42

21 A potential problem int *soup, *salad; soup = new int ( 42 ); salad = soup; delete soup; What does the picture of memory look like? What’s the potential problem?

22 Multi-dimensional Arrays We declare a static 2-dimensional array as char chArray1 [ 3 ] [ 4 ]; What we envision is chArray1 :

23 Static 2-D array in Memory What we get in memory is chArray1: row column We use double subscripts ( chArray [ 2 ] [ 1 ] ) to reference the elements by row/column. The compiler uses the row number, column number, number of columns and array type to calculate a memory address.

24 A dynamic 2-D array To dynamically allocate a 2-D array, we must create an array of pointers (the rows) in which each row element points to an array of elements (the columns). We need only declare the name of the dynamic array correctly char **chArray2;

25 char **chArray2 This declaration is read as “chArray2 is a pointer to a pointer to a char” In reality, the compiler doesn’t “know” if you plan to use chArray2 to point to one pointer to char or as the name of an array of many pointers to char. When we use it for dynamic 2-D arrays, we use it to point to an array of pointers to char

26 The code // the array of pointers for the rows chArray2 = new char* [ 3 ]; // now allocate the elements of each row for (int r = 0; r < 3; r++) chArray2 [ r ] = new char [ 4 ];

27 Dynamic 2-D Array in Memory chArray2: We can still use double subscript ( chArray [ 2 ] [ 1 ] ) to access the elements. The compiler uses pointer arithmetic to find the memory address.

28 delete’ing a dynamic array To deallocate a dynamically allocated multi- dimensional array, we must deallocate memory in the reverse order that it allocated for (int r = 0; r < 3; r++) delete [ ] chArray2[ r ]; delete [ ] chArray2;

29 Exercises for the student 1.Draw the results of the following code char *fischer; fischer = new char[3]; fischer[0] = ‘p’; fischer[1] = ‘k’; fischer[2] = ‘4’;

30 More exercises 2.Draw the results of the following code struct soprano { int x; char ch[3]; }; typedef soprano* alto[4]; soprano *jane; alto *joe[4]; jane = new soprano; jane->x = 1; jane-> ch[ jane->x ] = ‘G’; joe = new alto; joe[0] = new soprano; joe[0]->ch[2] = ‘Q’; joe[3] = jane;

31 Even more exercises 3. Write the typedefs, declarations and code that will create the picture below foo: 17 42

32 Still more 4. Write the typedefs, declarations and code that will produce the picture below foo: 16 bar: 16

33 And one last exercise 5. Write the typedefs, declarations and code that will produce the picture below. tom: 5