CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Pointers and Arrays C and Data Structures Baojian Hua
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Pointers OVERVIEW.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Writing a Good Program 8. Elementary Data Structure.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
C++ Memory Overview 4 major memory segments Key differences from Java
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.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Workin’ with Pointas An exercise in destroying your computer.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3,
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.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Programming Abstractions
Programming Abstractions
Programming Abstractions
Pointers and Memory Overview
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory.
The Stack.
Pointers and References
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics: Pointers! 1. Pointers and memory addresses 2. Dynamic memory allocation (new/delete) 2

Examples of the & operator int x = 42; int y; int a[3] = {91, -3, 85}; double d = 3.0; cout << x << endl; // 42 cout << &x << endl; // 0x7f8e20 cout << y << endl; // 17 cout << &y << endl; cout << a << endl; // 0x7f8e24 cout << &a[0] << endl; // 0x7f8e28 cout << &a[1] << endl; // 0x7f8e2c cout << &a[2] << endl; cout << &d << endl; // 0x7f8e30 Heap Stack 0 0x28F630 0x28F62c 0x28F628 0x28F624 0x28F620 0x28F61c 0x28F618

Examples of the & operator  Each memory address indexes one byte (8 bits). Can you deduce from the drawing at right how many bits are used to represent int and double, respectively? A. 4bits, 8bits B. 16bits, 16bits C. 16bits, 32bits D. 32bits, 64bits E. Other/none/more Heap Stack 0 int 0x28F630 0x28F62c 0x28F628 0x28F624 0x28F620 0x28F61c 0x28F618 int double

Pointers! int x = 42; int* p = &x; // pronounced “pointer to int” cout << p << endl; // 0x7f8e20 cout << *p << endl; // 42 *p = x*x; cout << x << endl; int a[3] = {5,6,0}; p = a; cout << p[1] << endl;  Pointer stores the address of a variable  * operator gets/sets the value stored at a memory address of that pointer NOTICE: 3 distinct meanings of * in C++!

Why do you think C++ makes you declare p as type int* (“pointer to int”) rather than just * (“pointer”)? int x = 42; int* p = &x; cout << p << endl; // 0x7f8e20 cout << *p << endl; // 42 *p = x*x; cout << x << endl; int a[3] = {5,6,0}; p = a; cout << p[1]<< endl; A. Who knows. There’s a lot about C++ that’s just inexplicable/weird/relic of ye olde tymes. B. int* and double* occupy different amounts of memory. We need to know how much space to allocate for a given pointer variable in the stack frame C. Other/none/more than one (assuming machine with 32-bit int and 64-bit double) Think like a language designer:

Pointer arithmetic  Operators +, -, ++, -- work on pointers  Incrementing a T* (pointer to type T) by 1 moves forward by sizeof(T) in memory  You can use [k] syntax to access memory k slots away from p (equivalent to +k )  (Corollary: An array variable is really just a pointer to the array's first element.) int a[4] = {91, -2, 85, 17}; int* p = a; // p = &a[0]; p[1] = 5; // a[1] = 5; p++; // p = &a[1]; cout << *p << endl; // 5 *(p + 2) = 26; // a[3] = 26; cout << p[2] << endl; cout << a[2] << endl; What prints here? A.26, 26 B.26, [other] C.[other], 26 D.[other], [other]

Software engineering tip: If not initializing a pointer right away, use NULL to force your program to crash (really!) if you try to use the pointer Common error: int* foo; … *foo = 555; Prevention: int* foo = NULL; … *foo = 555;

Common error: int* foo; … *foo = 555; Prevention: int* foo = NULL; … *foo = 555; Image is in the public domain: Heap Stack 0x28F620 Software engineering tip: If not initializing a pointer right away, use NULL to force your program to crash (really!) if you try to use the pointer

Dynamic memory allocation Potentially getting yourself in a heap of trouble! (haha)

The stack, scope, and the lifespan of memory void foo() { Lexicon english(“EnglishWords.dat”); for (string word : english) { cout << word << endl; } } // at this point stack frame is // popped and english disappears // forever  What if we want english to persist?  We could return it—does that make this particular english persist? No, makes a copy  Why can’t it make this particular english persist?  Why might copying be undesirable? main() Heap mymethod() x: xfac: factorial() n: 10 0 factorial() n: 9

new and delete  new asks for an allocation of memory on the heap  Heap doesn’t pop! Can stay there as long as we want!  Er… so when does it go away then?  There is no automatic time, so we have to be careful to manually let it go when we are done  delete

Dynamic memory allocation int * p1 = new int;//0x12 *p1 = 5; int * p2 = new int;//0x4 *p2 = 7; int * p3 = new int;//0x20 *p3 = ; // important phone # *p1 = *p2; cout << p1 << “ “ << *p1 << endl; p1 = p2; p1 = p3; delete p1; delete p2; // don’t delete p3 because we need that cout << *p3 << endl; What does this print? A.0x12, 5 B.0x4, 7 C.0x12, 7 D.0x4, 5 E.Other/none/ more

Dynamic memory allocation int * p1 = new int;//0x12 *p1 = 5; int * p2 = new int;//0x4 *p2 = 7; int * p3 = new int;//0x20 *p3 = ; // important phone # *p1 = *p2; cout << p1 << “ “ << *p1 << endl; p1 = p2; cout << p1 << “ “ << *p1 << endl; delete p1; delete p2; // don’t delete p3 because we need that cout << *p3 << endl; What does this print? A.0x12, 5 B.0x4, 7 C.0x12, 7 D.0x4, 5 E.Other/none/ more

Dynamic memory allocation int * p1 = new int;//0x12 *p1 = 5; int * p2 = new int;//0x4 *p2 = 7; int * p3 = new int;//0x20 *p3 = ; // important phone # *p1 = *p2; cout << p1 << “ “ << *p1 << endl; p1 = p2; cout << p1 << “ “ << *p1 << endl; delete p1; delete p2; cout << *p3 << endl; //print important phone # These last three lines… A.Looks good! B.Didn’t do enough deleting C.Did too much deleting D.Accessed memory after deleting E.Other/none/more

(again) Use NULL to ensure your program crashes (really!) Common error: delete foo; … *foo = 555; Prevention: delete foo; foo = NULL; … *foo = 555; Image is in the public domain: Heap Stack 0x28F620