Pointers and Dynamic Arrays

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
@ Zhigang Zhu, CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science.
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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Edgardo Molina, CSC212 Data Structure - Section AB Lectures 6/7 Pointers and Dynamic Arrays Instructor: Edgardo Molina Department of Computer Science.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Pointers Read Chapter 2 (P ). COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers and Dynamic Arrays
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Edgardo Molina, CSC212 Data Structure - Section AB Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Edgardo Molina Department.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
CS410 – Software Engineering Lecture #11: C++ Basics IV
Pointers Revisited What is variable address, name, value?
C++ Pointers and Arrays
8 Pointers.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Dynamically Allocated Memory
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 16-2 Linked Structures
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
Chapter 15 Pointers, Dynamic Data, and Reference Types
CSC212 Data Structure - Section FG
Constructors and Destructors
CISC/CMPE320 - Prof. McLeod
9-10 Classes: A Deeper Look.
C++ Pointers and Strings
CSC212 Data Structure - Section RS
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
CS410 – Software Engineering Lecture #5: C++ Basics III
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
Video: The Sound of Raining
SPL – PS3 C++ Classes.
Presentation transcript:

Pointers and Dynamic Arrays

Pointers and Dynamic Memory Pointer: is the memory address of a variable Memory address: at byte level Example: The integer i is located at memory address 990.

Pointer Variables Syntax Examples Type_Name *var_name; int *cursor; char *c1_ptr, *c2_ptr;

Address Operator Examples Dereferencing operator int *example_ptr; int i; Example_ptr = &i; //the address of i is put into the pointer variable example_ptr Dereferencing operator i = 42; example_ptr = &i; cout << i << endl; cout << *example_ptr << endl;

More Example int *example_ptr; int i; i = 42; example_ptr = &i; cout << i << endl; cout << *example_ptr << endl;

Pointer Assignment int i = 42; int *p1; int *p2; p1 = &i; p2 = p1; cout << *p1 << endl; cout << *p2 << endl;

Pointer Assignment int i = 42; int j = 80; int *p1; int *p2; p1 = &i; p2 = &j; *p2 = *p1; cout << *p1 << endl; cout << *p2 << endl;

NULL Pointer NULL – constant defined in cstdlib Means pointing to nothing

Dynamic Variables Not declared Created during execution of a program Memory is allocated using an new operator in a special memory location called the heap Examples: int *n; n = new int; *n = 5; // Can be combined in 1 statement int *n = new int(5);

Dynamic Arrays Dynamically allocate entire array int *a = new int[20]; Returns a pointer to the first element in the array

Failure of new Operator new operator fails if insufficient memory is available in the heap bad_alloc exception is thrown

Delete Operator When dynamic variable is no longer needed – need to release its memory delete operator Example int *n = new int(5); …… delete n; double* a = new double[6]; delete [] a;

Define Pointer Types Example typedef int* int_pointer; int_pointer i_ptr;

Pointers as Parameters Passing pointers as value parameters Pointer cannot be changed – object pointed to can be changed Example void confuz (int* n) { *n = 2; }

Arrays as Parameters Array name is treated as a pointer to first element in array Size of array is not passed in as part of the array – must be done separately Example: void make_it_all_42(int a[], size_t n) { for (size_t i = 0; i < n; i++) a[i] = 42; } int *numbers = new int[10]; make_it_all_42(numbers, 10);

const Parameters void confuz (const int* n); Integer being pointed to by n cannot be modified void confuz (const int a[], size_t n); No element of array a can be modified

Pointer Reference Parameters Pointer parameter can be modified since passed by reference Example: void confuz (int*& n) { n = new int (5); }

Dynamic Bag Implementing the bag class with a dynamic array private: value_type *data; size_type used; size_type capacity;

Copy Constructor A copy constructor is a constructor with exactly one argument, and the data type of the argument is the same as the constructor’s class Need to make sure that the object and its copy are independent from each other (deep copy) For member data that are pointers – copy objects being pointed to (recursively) Shallow copy – only copy pointers bag(const bag& source); bag y(x); //initialize y as a copy of x

Overloading Assignment Operator Need to use same copying concept as with copy constructors void operator =(const bag& source); bag y = x; //initialize y as a copy of x

Destructor Deallocate object's dynamic memory Name – class name preceded by ~ No parameters & no return type Automatically called when object is destroyed bag::~bag( ) { delete [ ] data; }

Dynamic Class Design Some of the member data are pointers Some member functions allocate & deallocate dynamic memory Assignment operator needs to be overloaded Copy constructor must be constructed Destructor must be constructed

C strings Null character (‘\0’) terminated arrays of characters char s[n] – can hold at most n-1 characters Empty string char quiet[20]=“”; strcpy strcmp strcat strlen

STL string class Added in later versions of C++ Designed to address weaknesses of C version