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.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
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.
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.
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.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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.
Object-Oriented Programming in C++
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
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.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
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.
Dynamic Storage Allocation
Chapter 9: Pointers.
Pointers Revisited What is variable address, name, value?
Pointer.
Pointers and References
Dynamic Memory Allocation
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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 – images and image representations

2 Array Example 1 Write a program that finds the largest number in a given collection of keys. Assume the numbers are stored in an array. int max (int[] A, int size)

3 Array Example Write a program that finds the largest number in a given collection of keys. Assume the numbers are stored in an array. int max (int[] A, int size) { if (size == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }

4 Implementation using a vector int max (vector A, int n) { if (n == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }

5 Notion of time complexity int max (int[] A, int n) { if (n == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; } What is the total number of operations performed by the above procedure (as a function of n )? Assignment, comparison, return – each costs one unit.

Pointers A pointer is a variable used to store an address The declaration of a pointer must have a data type for the address the pointer will hold (looks like this): int *ptr; char *chptr;

Location Behavior A variable is a location When locations are used in code, they behave differently depending on whether or not they are used on the left side of an assignment If not used on the left side of an assignment, the value at the location is used When used on the left side of assignment, the location itself is used

Address-of operator An address can be assigned to a pointer using the address-of operator &: int *ptr; int x; ptr = &x;

x ptr Addresses x ptr Pointer variable, its value

Dereference Operator The dereference operator, *, is used on a pointer (or any expression that yields an address) The result of the dereference operation is a location (the location at the address that the pointer stores) Therefore, the way the dereference operator behaves depends on where it appears in code (like variables)

A simple example int x = 3, *ptr; ptr x3

Assignment ptr x3 ptr = &x;

ptr x3 Result of assignment ptr = &x;

Another assignment ptr x3 y y = *ptr + 5;

15 Another assignment ptr x3 8 y y = *ptr + 5;

More assignment int z = 5; ptr x3 y8 z5

effect of assignment *ptr = 10 + z; ptr x y8 z5 3

x y8 z5 15 *ptr = 10 + z;

ptr x y8 z5 15 Note that the value of x changes even though this line of code doesn’t contain x

20 Exercise: What is the output produced by the following program? #include int main() { int * p; int * q; int x; p = &x; *p = 6; cout << x << endl; q = p; *q = 8; cout << *p << endl; q = new int; *q = 9; cout << *q << endl; return 0; }

Arrays The address of an array is the same as the address of the first element of the array. An array’s name (without using an index) contains the address of the array. The address of the array can be assigned to a pointer by assigning the array’s name to the pointer. An array name is not a pointer because the address in it cannot be changed (the address in a pointer can be changed).

The [ ] Operator When an array is indexed, [ ] is an operator. For example, nums[3] produces the result *(nums + 3). C++ produces an address from the expression nums + 3, by: –Noting what data type is stored at the address that nums contains –Multiplying by 3 the number of bytes in that data type –Adding the product onto the address stored in nums After the address is produced from nums + 3, it is dereferenced to get a location.

The Heap The heap is a special part of RAM memory reserved for program usage. When a program uses memory from the heap, the used heap memory is called dynamically-allocated memory. The only way to dynamically allocate memory (use memory in the heap) is by using the new operator.

The new Operator The new operator has only one operand, which is a data type. The new operation produces the address of an unused chunk of heap memory large enough to hold the data type (dynamically allocated) In order to be useful, the address must be assigned to a pointer, so that the dynamically allocated memory can be accessed through the pointer: int *ptr; ptr = new int; *ptr = 5;

int *ptr; ptr

ptr = new int; ptr

ptr = new int; ptr

ptr = new int; ptr Found a block in heap

ptr = ; ptr

ptr = ; ptr

*ptr = 5; (what happens?) ptr

ptr *ptr = 5;

Dynamically-allocated memory Has no name associated with it (other locations have variable names associated with them) Can only be accessed by using a pointer The compiler does not need to know the size (in bytes) of the allocation at compile time The compiler does need to know the size (in bytes) of any declared variables or arrays at compile time

34 Exercise: What is the output produced by the following program? #include int main() { int * p; int * q; int x; p = &x; *p = 6; cout << x << endl; q = p; *q = 8; cout << *p << endl; q = new int; *q = 9; cout << *q << endl; return 0; }

Memory Leak First, pointer ptr is assigned the address of dynamically allocated memory. ptr

Pointer assigned again ptr ptr = new int; (what happens?)

ptr = new int; ptr Unused block found in heap

Pointer assignment changes ptr

ptr Pointer assignment

ptr The address of this block is not stored anywhere, but it hasn’t been freed – memory leak Memory location inaccessible

Avoiding Memory Leak When you want to change the address stored in a pointer, always think about whether or not the current address is used for dynamically-allocated memory If it is (and that memory is no longer useful), use the delete operator before changing the address in the pointer; for example, delete ptr;

Pointers to Objects Pointers can be used to point to objects or arrays of objects: Check *chkptr = new Check; Check *chkarrptr = new Check [10]; The delete operator is used the same way: delete chkptr; delete [ ] chkarrptr;

Running out of Heap Memory We can run out of heap memory if: –We write poor code that continually allows memory leak while constantly using the new operator –OR … –If we use excessive amounts of heap memory If the new operator cannot find a chunk of unused heap memory large enough for the data type, the new operation throws an exception