Pointers Revisited What is variable address, name, value?

Slides:



Advertisements
Similar presentations
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Advertisements

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.
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];
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
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.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
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.
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.
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++
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Pointers and Dynamic Memory Allocation
Memory Management.
Object Lifetime and Pointers
Dynamic Storage Allocation
Pointers and Dynamic Arrays
Pointers and Dynamic Arrays
Pointers.
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Pointers, Polymorphism, and Memory Allocation
Pointers.
Pointers and Pointer-Based Strings
COMP 2710 Software Construction Pointers
Dynamic Memory CSCE 121 J. Michael Moore.
Values – also known as scalars (eg. int, double, char, float)
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Pointers and Dynamic Variables
Dynamically Allocated Memory
Pointers and References
Dynamic Memory Allocation
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
9-10 Classes: A Deeper Look.
Pointers and Pointer-Based Strings
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Submitted By : Veenu Saini Lecturer (IT)
Standard Version of Starting Out with C++, 4th Edition
Pointers and References
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Pointers Revisited What is variable address, name, value? What is a pointer? How is a pointer declared? What is address-of (reference) and dereference operators? How can a pointer be assigned a value? How can a value of a memory location referred to by a pointer be accessed? What is a constant pointer? What is a pointer to a constant? What is the relationship between array name and a pointer? What is the operation to move pointer to the next/previous memory location? What is null pointer? What is lose pointer problem? Can pointers be used with objects? How can a method be invoked on an object using pointer? what is -> operator?

Dynamic Memory Allocation

Why Dynamic Memory when a variable is declared - a certain memory area (sufficient to hold the variable of this type or class) is allocated not all memory can be allocated or efficiently allocated at the compilation time we are manipulating an set of data of arbitrary size what if we allocate an array too small? what if we allocate an array too large? dynamic memory allocation – memory allocation at execution time under the direct control of programmer more flexible since the program can claim and release memory as needed and potentially can get as much memory as the computer resources allow heap - the system data structure for dynamic memory allocation what is program (or call) stack again? heap, like program stack, is separate for every program and is removed when the program finishes

new and delete new and delete - operations used for dynamic memory allocation new - allocates a nameless variable of specified type (or class) and returns a pointer to it int *ip; // declares pointer ip = new int; // ip points to integer variable note that the variable has no name and the only way to access the variable is though a pointer: cin >> *ip; *ip += 20; cout << *ip; new may take a parameter to initialize the variable with ip = new int(5); // 5 assigned to the var delete - releases memory back to heap for reuse delete ip; note: the memory pointed to by ip is deallocated, not the pointer variable itself memory allocation types static variable/constant – location known at compile time: literal constants, global variables, static variables, allocated in special place for static vars. automatic variable – with scope: local variables, parameters, temp. variables, allocated on program stack dynamic variable - created with new, allocated in heap

Memory Leak Problem pointer that points to the dynamic variable is the only way to access this variable if the pointer is reassigned the dynamic variable may not be accessed. This is the memory leak problem int *ptr = new int; ptr = new int; // error - memory leak is this a memory leak? int *ptr1,*ptr2 = new int; ptr1=ptr2; ptr2 = new int; does this code have a problem? delete ptr; *ptr=5;

Pointers and Arrays array name is equivalent to: base_type * const that is array name is a pointer that cannot be changed array name points to the first element of the array array name can be used as pointer pointer can be used as an array name int a[10], *p1, *p2; p1=a; // where does p1 point now? p1[2]=5; // which element does p1 access a=p2; // is this legal? p1[20]; // is this legal?

Dynamic Arrays arrays can be created dynamically just as scalar variables. new has to be passed the number of elements of the array int *p1, *p2, num; p1=new int[10]; the number of array elements need not be constant: cin >> num; p2=new int[num]; p2= new int[0]; // operates correctly, sometimes useful new returns the pointer to the first element of the dynamically allocated array. This pointer can be used just like regular array name: p1[2]=42; unlike regular array name, this pointer is not constant and can still be changed p1=p2; delete has special syntax for array deallocation: delete [] p1; the pointer passed to delete does not have to be the same that receives the value returned by new how does the computer know how many elements to delete? – part of dynamic array implementation

Memory Leak with Dynamic Arrays int *p = new int [5]; for (int i = 0; i < 5; ++i) p[i] = i; p = new int [5]; p 1 2 3 4 p 1 2 3 4 — these locations cannot be accessed by program

Pointers and Functions // passing pointer by value void funcVal(int *p){ *p = 22; p = new int(33); } // passing pointer by reference void funcRef(int *& p){ *p = 44; p = new int(55); // passing pointer to pointer, superseded by above: seldom used void funcRefRef(int **pp){ **pp = 66; *pp = new int(77); // returning pointer int *funcRet(){ int *tmp = new int(88); return tmp; pointers can be passed as parameters to and returned by functions

Dynamic Objects objects can be allocated dynamically class myclass { public: myclass(int n){data=n;} int getdata() const {return data;} private: int data; }; the object of class myclass can be allocated as follows: myclass *mp1; mp1 = new myclass(5); when new is passed parameter - constructor is invoked the object can then be used as regular object: cout << mp1->getdata(); and destroyed: delete mp1;