Lecture 10: 2/17/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Kernighan/Ritchie: Kelley/Pohl:
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
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.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
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.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Pointers CSE 2451 Rong Shi.
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
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.
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.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
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.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
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.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Dynamic Storage Allocation
Standard Version of Starting Out with C++, 4th Edition
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers Revisited What is variable address, name, value?
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Memory Allocation CS 217.
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS148 Introduction to Programming II
Presentation transcript:

Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 10: 2/17/2003

CS148 Spring Outline n Pointers and structs n Pointer expressions n Dynamic Data Chapter 15 ( )

Lecture 10: 2/17/2003CS148 Spring Pointers and Structs struct PatientRec { Int idNum; Int height; Int weight; }; PatientRec patient; // struct variable PatientRec* patientPtr = &patient; // pointer variable to a struct variable PatientRec* patPtrArray[20]; // array of pointers n *PatientPtr struct variable of type PatientRec n (*patientPtr).weight weight member of a struct of type PatientRec (dereferencing and member selection) n () necessary because dot operator has higher precedence than the dereference operator

Lecture 10: 2/17/2003CS148 Spring Pointers and Structs arrow operator (->) (member selection operator) PointerExpression->MemberName Equivalent to (*PointerExpression).MemberName (*PatientPtr).weight is equivalent to PatientPtr->weight

Lecture 10: 2/17/2003CS148 Spring Pointers and Structs Example patientPtr->idNum = 5555; patientPtr->height = 64; patientPtr->weight = 114; patPtrArray[3]->idNum = 6666; patPtrArray[3]->height = 80; patPtrArray[3]->weight = 185; 5555 patientPtr*patientPtr *patPtrArray[3] patPtrArray [0] [1] [2] [3]

Lecture 10: 2/17/2003CS148 Spring Pointer Expressions n A pointer expression is a combination of pointer variables, pointer constants, operators and parentheses. n One literal pointer constant 0 intPtr = 0; //null pointer, points to nothing n For better program readability and documentation use named constant NULL

Lecture 10: 2/17/2003CS148 Spring Pointer Expressions n Array name without any index brackets considered to be constant pointer expression int arr[100]; int* ptr; ptr = arr; has same effect as ptr = &arr[0]; n Indexing a Pointer (pointer points to array) int numbers[30]; ZeroOut(numbers,30); void ZeroOut (int *arr, int size) { int i; for (i = 0; i < size ; i++) arr[i] = 0; }

Lecture 10: 2/17/2003CS148 Spring Pointer Expressions n Example (Pointer Assignment) int x = 10; int y = 20; int* ptr_x = &x; int* ptr_y = &y; 10 ptr_xx 20 ptr_yy 10 ptr_xx 20 ptr_yy ptrx = ptry;

Lecture 10: 2/17/2003CS148 Spring Summary of Pointer Operations n = Assignment ptr = &var n ptr1 = ptr2 n ptr = NULL n * Dereference *ptr n ! Logical NOT !ptr n (1 if ptr is null pointer) n ==, !=,, and >= n Relational operators ptr1 == ptr2 n -> Member selection ptr->height n Pointer arithmetic not covered here ptr++,ptr+5

Lecture 10: 2/17/2003CS148 Spring Pointers and function parameters n By Default, C++ passes function parameters by value n Pointers can be used to pass function parameters by reference n Can achieve the same using reference variables. Swap(alpha,beta); void Swap(int x, int y) { //this code does not work //copies of alpha and beta values are passed //local contents of x and y are swapped //actual parameters remain unchanged int temp = x; x = y; y = temp; } Swap(&alpha,&beta); void Swap(int* x, int* y) { //pass by reference //addresses of alpha and beta are passed //actual parameters changed int* temp = *x; *x = *y; *y = *temp; }

Lecture 10: 2/17/2003CS148 Spring Dynamic Data n Pointers used to manipulated dynamic variables n Dynamic variables explicitly allocated and deallocated during program execution n Two special operators new and delete n Variable Lifetime = time between execution of new and delete n Variables allocated from within a region of memory set aside for dynamic variables (Free Store) n Optimize the program’s memory consumption. The size of an array or a string of characters might not be known, or the maximum size might be very large compared to the actual used size.

Lecture 10: 2/17/2003CS148 Spring Dynamic Data Allocation/Deallocation Example #include int* intPtr = new int; char* name = new char[6]; intPtr name ? Free Store ?????? 05 intPtr name 400 Free Store ‘X’‘y’‘Z’‘\0’?? 05 undefined intPtr Free Store ‘X’‘y’‘Z’‘\0’?? 05name *intPtr = 400; strcpy(name,”XyZ”); delete intPtr;

Lecture 10: 2/17/2003CS148 Spring Dynamic Data Pitfalls int *ptr1 = new int; int *ptr2 = new int; *ptr2 = 60; ? ptr1*ptr1 60 ptr2*ptr2 *ptr1 = *ptr2; ptr1 = ptr2; delete ptr2; 60 ptr1*ptr1 60 ptr2*ptr2 60 ptr1 60 ptr2*ptr2 Inaccessible object (memory leak) 60 ptr1 (dangling pointer) ?? ptr2