A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
 Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 6 Data Types
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
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.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Workin’ with Pointas An exercise in destroying your computer.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Week 12 Methods for passing actual parameters to formal parameters.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1. Generic Pointer 2. NULL Pointer 3. Wild Pointer 4. Dangling Pointer.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Compound Types: References, Pointers Where are we going with this?
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Variables Mr. Crone.
Learning Objectives Pointers Pointer in function call
Pointers Psst… over there.
Basic notes on pointers in C
Pointer Data Type and Pointer Variables
References and Pointers
Pointers Psst… over there.
Dynamic Memory Allocation
Pointer Basics Psst… over there.
CS 2308 Exam I Review.
Dynamic Memory Copy Challenge
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Dynamic Memory A whole heap of fun….
Pointers Lecture 1 Thu, Jan 15, 2004.
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
A simple function.
Dynamic Memory Copy Challenge
Pointer Data Type and Pointer Variables
Pointer Basics Psst… over there.
Pointer Data Type and Pointer Variables
Introduction to Pointers
Presentation transcript:

A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used to hold a pointer. It is a special data type. Pointers and Dynamic Memory

A pointer variable can be assigned a pointer (memory address). We can declare a pointer variable: int * myIntPtr; Type of data pointed to (int), pointer symbol (*), variable name (myIntPtr). Pointers and Dynamic Memory

We can assign a memory address (pointer) to the variable: (& is called the address-of operator) myIntPtr = &myInt; Where: int myInt was declared earlier in the program. Pointers and Dynamic Memory

We can refer to the value in the location pointer to by myIntPtr with the dereferencing operator * as follows: cout << *myIntPtr << endl; If we had previously executed the statement int myInt =42; We would see ‘42’ in the output. Pointers and Dynamic Memory

Pointers can be set with the usual assign operator. int i = 42; int * p1, *p2; p1 = &i; 42 int *p1 int *p2 int i Before

Pointers and Dynamic Memory Pointers can be set with the usual assign operator. int i = 42; int * p1, *p2; p1 = &i; // p2 = p1; 42 int *p1 int *p2 int i After

Pointers and Dynamic Memory Contents of pointer variables can be set with the assign operator. int i = 42; int j = 17 int * p1, *p2; p1 = &i; p2 = &j; 42 int *p1 int *p2 int i Before 17 int j

Pointers and Dynamic Memory Contents of pointers variables can be set with the assign operator. *p1 = *p2; 17 int *p1 int *p2 int i After 17 int j Value of location pointed to by p2 copied to location pointed to by p1

Pointers and Dynamic Memory The real power of pointers is seen when they are used to point to dynamically allocated variables. Let’s see why.

Pointers and Dynamic Memory Dynamic variables are used just like ordinary static variables except: They are not declared, so they have no identifiers like static variables do. They are created during run time, not when the program is compiled.

Pointers and Dynamic Memory Storage for these variables comes from an area of memory called the free store or the heap. The creation of new dynamic variables is called memory allocation and the memory is called dynamic memory. C++ uses the new operator create a dynamic variable.

Pointers and Dynamic Memory Here are the steps: int * myIntPtr; // create an integer pointer variable myIntPtr = new int; // create a dynamic variable // of the size integer new returns a pointer (or memory address) to the location where the data is to be stored.

Pointers and Dynamic Memory Free Store (heap) myIntPtr int * myIntPtr myIntPtr = new int;

Pointers and Dynamic Memory Free Store (heap) myIntPtr int * myIntPtr myIntPtr = new int; *myIntPtr = 123; Use pointer variable as before

Pointers and Dynamic Memory We can also allocate entire arrays with the new operator. These are called dynamic arrays. This allows a program to ask for just the amount of memory space it needs at run time.

Pointers and Dynamic Memory Free Store (heap) myIntPtr int * myIntPtr; myIntPtr = new int[4]; Notice that the pointer symbol is understood, no * is used to reference the array element myIntPtr[1] = 325;

Pointers and Dynamic Memory The new operator gets memory from the free store (heap). When you are done using a memory location, it is your responsibility to return the space to the free store. This is done with the delete operator. delete myIntPtr; // Deletes the memory pointed delete [ ] arrayPtr; // to but not the pointer variable

Pointers and Dynamic Memory Dynamic memory allocation provides a more flexible solution when memory requirements vary greatly. The memory pool for dynamic memory allocation is larger than that set aside for static memory allocation. Dynamic memory can be returned to the free store and allocated for storing other data at later points in a program. (reused)

Pointers and Arrays as Parameters int *main_ptr; main_ptr = new int; set_value(main_ptr); void set_value(int * tempPtr) { *tempPtr = 222; } 222 main_ptr tempPtr

Pointers and Arrays as Parameters int *main_ptr; main_ptr = new int[4]; set_value(main_ptr); void set_value(int tempPtr[]) { tempPtr[1] = 222; } 222 main_ptr tempPtr

Pointers and Arrays as Const Parameters int *main_ptr; main_ptr = new int[4]; set_value(main_ptr); void set_value(const int tempPtr[]) { tempPtr[1] = 222; } main_ptr Not allowed

Pointers and Dynamic Memory Sometimes we may want a function to change a pointer so that it points to a different location This is accomplished by using a reference parameter that is a pointer type void change_location(int* & refPtr)

Pointers and Dynamic Memory We often use a typedef statement to simplify the cumbersome syntax. typedef int* inPtr; inPtr myIntPtr; void functionEx(inPtr & localPtr);

Pointers and Dynamic Memory Some things to be careful with when using pointers. Pointers should always point to something. When a object pointed to is no longer needed, the memory should be freed with delete and the pointer should be assigned the special value null, defined in the stddef.h header file. delete myIntPtr; // return memory to free store myIntPtr = null; // point to special “nothing” value.

Pointers and Dynamic Memory A pointer not pointing to an object is considered “unassigned”. An unassigned pointer variable must not be dereferenced. A “null” pointer value should not be dereferenced. A pointer left pointing is called a dangling pointer.

Pointers and Dynamic Memory Dangling pointers can occur when two or more pointers point to the same object and one of the pointers is used with the delete operator. 42 int *p1 int *p2 42 int *p1 int *p2 Delete p1; Returned to free store A dangling pointer

Pointers and Dynamic Memory An inaccessable memory location, also called a memory leak, results from the reassignment of a pointer without first releasing the memory it pointed to. An inaccessable object 42 int *p1 int *p2 17 p2 = p1; 42 int *p1 int *p2 17

Pointers and Dynamic Memory An inaccessable memory location can also be caused by inappropriate use of new operator. An inaccessable object 42 int *p1 p1 = new int; 42 int *p1

Pointers and Dynamic Memory Pointers also introduce potential problems with the behavior of the automatic assignment operator, the copy operation, and the comparison operation. The automatic behavior produces what is called a shallow copy. Both pointers point to the same object. What we want is a deep copy. We want a copy of the data not just the pointer.

Pointers and Dynamic Memory Free Store (heap) myIntPtr int * myIntPtr; myIntPtr = new int[4]; int * myIntPtr2; myIntPtr2 = myIntPtr; myIntPtr Not the default behavior of ‘=‘ What we wanted.

Pointers and Dynamic Memory Free Store (heap) myIntPtr int * myIntPtr; myIntPtr = new int[4]; int * myIntPtr2; myIntPtr2 = myIntPtr; myIntPtr2 What we got!

Pointers and Dynamic Memory To handle this and other dynamic data situations, we must add additional member functions to any class that uses dynamic data. Some additional functions would want: We must overload the assignment operator. We must provide a copy constructor. We also need to add a destructor member function.

Pointers and Dynamic Memory Let’s look at the copy constructor. The principles are the same for the assignment operator overload. A copy constructor is a special constructor called when a new instance of a class is created from an existing object of that class. DynamicClass newClass(oldClass); DynamicClass newClass = oldClass;

Pointers and Dynamic Memory Class DynamicClass { public: DynamicClass (const DynamicClass & oldClass); void operator =(const DynamicClass & oldClass); ~DynamicClass (); … };

Pointers and Dynamic Memory DynamicClass::DynamicClass (const DynamicClass& oldClass) { data = new Item[someSize]; for (int i=0; i < oldClass.currentSize; i++) { data[i] = oldClass[i]; }

Pointers and Dynamic Memory Free Store (heap) data A B C D A B C D oldClass

Pointers and Dynamic Memory Now let’s apply what we’ve learned to resizing a dynamically allocated array. When the capacity of the original array is used up, we want to allocate a new larger array and copy the data from the original array to the new larger array. After the data has been copied, we can delete the original array and return the memory to free store.

Pointers and Dynamic Memory data A B C D A B C D oldClass 4 5 6

Pointers and Dynamic Memory data A B C D A B C D oldClass delete [ ] oldClass; Return memory

Pointers and Dynamic Memory Class DynamicClass { public: DynamicClass (const DynamicClass & oldClass); void operator =(const DynamicClass & oldClass); ~DynamicClass (); … };

Pointers and Dynamic Memory When we are finished using a class instance, we must free any memory pointed to by variables in the class. This is done automatically by invoking the code contained in the class destructor. Failure to do so creates memory leaks. Like the constructor, the destructor does not return any data type. It has the same name as the class name but is preceded by the ‘ ~ ’ symbol. DynamicClass:: ~ DynamicClass() { }

Pointers and Dynamic Memory The use of pointers and dynamic variables is an important problem solving tool. It gives the class builder flexibility in managing memory. It’s use does require more care on the part of the programmer, since control is now in the hands of the programmer and not the system. However, the benefits generally outweigh the disadvantages and C++ programs make heavy use of dynamic memory.