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.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

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.
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.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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.
Pointers “Absolute C++” Section 10.1
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
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 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.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
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.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
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.
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.
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
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
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Review 1 List Data Structure List operations List Implementation Array Linked List.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
System Programming Practical Session 7 C++ Memory Handling.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic 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.
Learners Support Publications Operators.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Dynamic Storage Allocation
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
COMP 2710 Software Construction Pointers
Pointers Revisited What is variable address, name, value?
Values – also known as scalars (eg. int, double, char, float)
Dynamic Memory Allocation
Pointer Basics Psst… over there.
Pointers and Dynamic Variables
Dynamic Memory A whole heap of fun….
Pointers and Dynamic Variables
What have we learned about when we learned about function parameters?
Dynamic Memory.
Pointer Basics Psst… over there.
Dynamic Memory CSCE 121.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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 is actually using pointer.

Pointer Variables Declare a pointer: type *name; Ex: int *p; // declares *p as an integer and p as the address of that integer. // Therefore p = &(*p) Multiple declaration: type *name1, *name2; Ex: int *p, *q; // p and q are pointers Mixed declaration: Ex: int *p, q, *r; // p and r are pointers, q is an integer Pointers can be initialized: Ex: int a, *p = &a; // means pointer p=&a, NOT *p=&a int *p = &a, a; // NO! a is still not declared at time of assigning Pointer must be declared as pointer to a specific type. Can’t have integer pointer points to double pointer.

Pointer Operators * (not in the declaration): – When used in front of a pointer variable produces the value of the variable it points to. – Called dereferencing operator. &: – When used in front of a ordinary variable produces the address of that variable. (or a pointer that points to the variable) =: – Assign a pointer to the address of another variable of the same type – Assign a pointer to another pointer int *p, q, *r; r = &q; // q must declared before r so the address for q exists p = r; // r already points to an existing address. – Assign content of a pointer to content of another pointer of the same type *p = *q; – Can’t directly assign a pointer to an integer. It will point to an absolute address in memory. Too dangerous. It can overwrite the value at that specific memory location used by other programs. p = 12345; – Can’t directly assign a pointer to an expression (no address). It will point to an absolute address in memory. Too dangerous. It can overwrite the value at that specific memory location used by other programs. p = &(q+1);

The new operation Allows creating a new pointer variables during execution. Called dynamic instantiation. The variable is then called dynamic variable. int *p; p = new int; *p = 100; p? p? p100

The NULL pointer defined in cstddef.h Global and static pointers are initialized to zero Cannot be dereferenced If no memory is available when the new command is executed, a NULL value (or std::badalloc exception) is returned

Basic pointer Manipulations int *p, q=20, *r; // p and r are pointers, q is an integer p = new int; *p = 42; r = p; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; p? p? r? q 20 r? q p42 ? q 20 p42q 20 r r *p == 42 *r == 42

Basic pointer Manipulations (cont.) *r = 52; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; *r = *p + 7; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; p = new int; *p == 59 *r == 59 p52q 20 r p59q 20 r p?q r59 *p == 52 *r == 52

Basic pointer Manipulations (cont.) *p = 88; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; r = &q; cout << "*p == " << *p << endl; cout << "*r == " << *r << endl; What is the value of *r if q is changed to 40? p88q 20 r59 *p == 88 *r == 59 p88 q20 r59 *p == 88 *r == 20 40

Basic Memory Management (Heap or Freestore) A special area of free memory reserved for dynamic variables. When a pointer is created (with new operator), it gets an address from the heap. If the heap is empty, a call to new return a NULL pointer. Then, the call to new fails.

Basic Memory Management (delete operator) When the memory the pointer points to is no longer needed, you must use delete operator to return freed memory to the heap. The memory then can be reused for creating new dynamic variables. If the pointer variable is out of scope, the memory used by undeleted pointer will be still occupied. It causes memory leak.

Basic Memory Management (dangling pointers) When the delete operator is applied to a pointer variable, the dynamic variable it is point to is destroyed. Then, the value of the pointer variable is undefined. And the pointer becomes dangling pointer. Any pointer point to the same dynamic variable also becomes a dangling pointer. So, remember set the pointer to NULL.

Basic pointer Manipulations (dangling pointers) int *p, *q; p = new int; *p = 10; p = q; p? ?q p? ?q p10 q p ?q p delete p; p = NULL; q = NULL; ? ?q pNULL ?q p q 10 p

Type define (typedef) Allows you to give a name to a type for clarity. Ex: typedef int* IntPtr; IntPtr pointer1, pointer2;

Static Variables Static variables: Keyword static in front of variable in a function makes its viability global but its visibility local. Initialized on first function call, it maintains it’s value through additional calls until the program ends. void tally() { static int count = 0; cout << ++count << endl; } void main () { for (int i=0; i<5; i++) tally(); }

Dynamic and Automatic Variables Dynamic: created and destroyed by the programmer. (pointers) Automatic: automatically created on entrance to block and destroyed on exit. (Ordinary variables) Global: Outside of main. Can be seen by any other file or program.