C++ Programming. Pointers and Memory Management. M1 Math Michail Lampis

Similar documents
C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

Pointers and References

CA31-1K DIS. Pointers. TA: You Lu

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Pointers, Dynamic Data, and Reference Types

CS201- Introduction to Programming Current Quizzes

Pointers and Memory Allocation p. 1. Brooklyn College. Michael Lampis. CISC 3130 Notes. Pointers and Memory Allocation

Exam 3 Chapters 7 & 9

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

C++ for Java Programmers

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

For example, let s say we define an array of char of size six:

Introducing C++ to Java Programmers

Memory and Pointers written by Cathy Saxton

Understand Execution of a Program

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Data Structures CSci 1200 Test 2 Questions

FORM 2 (Please put your name and form # on the scantron!!!!)

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

1 Unit 8 'for' Loops

Structured Data. CIS 15 : Spring 2007

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

C++ Programming Lecture 4 Software Engineering Group

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Intermediate Programming, Spring 2017*

CS 11 C track: lecture 5

BITG 1113: Array (Part 1) LECTURE 8

The Stack, Free Store, and Global Namespace

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CSE 307: Principles of Programming Languages

CSE 100: C++ TEMPLATES AND ITERATORS

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370

Ch. 11: References & the Copy-Constructor. - continued -

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Unit #3: Recursion, Induction, and Loop Invariants

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CSC 2400: Computer Systems. Using the Stack for Function Calls

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

a data type is Types

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Technical Questions. Q 1) What are the key features in C programming language?

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

7.1 Optional Parameters

Lecture Notes on Memory Layout

vector and Free Store

Dynamic Allocation in C

In Java we have the keyword null, which is the value of an uninitialized reference type


EE 355 Lab 4 - Party Like A Char Star

CS 103 Lab - Party Like A Char Star

CSC 1600 Memory Layout for Unix Processes"

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Vector and Free Store (Vectors and Arrays)

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Lecture on pointers, references, and arrays and vectors

Chapter 17 vector and Free Store

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Dynamic Allocation of Memory

Dynamic Allocation in C

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

CS201 Latest Solved MCQs

6. Pointers, Structs, and Arrays. 1. Juli 2011

Pointers and Dynamic Memory Allocation

Programming Abstractions

Advanced Programming & C++ Language

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

CS 103 Lab 6 - Party Like A Char Star

Lecture 2, September 4

LECTURE 03 LINKED LIST

Unit #2: Recursion, Induction, and Loop Invariants

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS201 Some Important Definitions

ESC101 : Fundamental of Computing

Chapter 9. Pointers and Dynamic Arrays

Chapter 1 Getting Started

CSI33 Data Structures

Transcription:

C++ Programming Pointers and Memory Management M1 Math Michail Lampis michail.lampis@dauphine.fr

Dynamic Memory Allocation Data in your program lives (mostly) in two areas The stack The heap So far, we have been using only the stack

The stack The stack contains local variables of each block/function call The reason it is called a stack is that it grows/shrinks in one direction (up/down) The stack follows the execution path/function calls

The stack and recursive functions Recall the factorial function int fact(int n){ } if(n<2) return 1; return n*fact(n-1); This function will allocate n different integers before it calculates anything...

The stack Good about the stack Fast Clean Bad about the stack Data is lost when a function terminates (except things we return) Often, memory needs and execution flow do not go in parallel

The heap The heap allows us to store data in arbitrary places in memory Idea: a function f can start building some object The caller function/main program should have access to this object when f terminates Heap manipulation in C++ is done with new delete

An example program You have a function that calculates two magic numbers How to return them both to main? int magic(){ } int n1 = 42; int n2 = 2112; return n1; //???

Solution: first attempt Why not return an array? int [ ] magic(){ } int n[2] = {42, 2112}; return n;

Solution: first attempt Why not return an array? int * magic(){ } int n[2] = {42, 2112}; return n;

Solution: first attempt Why not return an array? int * magic(){ int n[2] = {42, 2112}; return n; } int *p = magic(); cout << p[0] << p[1] << endl;

How about this? //All in one function! int n[2] = {42, 2112}; return n; int *p = n; cout << p[0] << p[1] << endl;

Shrinking stack The problem with the first solution is that the memory for n is lost when the function terminates The return statement (correctly) return the address of n[0] But this address points to a place in memory which may not hold the value 42 any more!

new We can allocate heap memory using new Syntax: new T, returns a pointer to a (new) place in memory that can hold data of type T. This is why you NEED to understand pointers int * p = new int; //p is pointing to a new int Syntax: new T[size], returns a pointer to a (new) array of size elements of type T int * p = new int[2]; //p is pointing to a new array

Back to our problem int * magic(){ //int n[2] = {42, 2112}; int * n = new int[2]; n[0] = 42; n[1] = 2112; return n; } int *p = magic(); cout << p[0] << p[1] << endl;

Managing the heap The new operator gives us great power! We can allocate memory whenever we need it The memory stays around as long as we need it With great power comes great responsibility! Stack memory is cleaned up automatically, but we must take care of cleaning up heap memory we don't need! We also need to make sure we don't lose memory we have

What is the problem? int * magic1(){...} int * magic2(){...} int * magic3(){...} int *p = magic1(); cout << p[0] << p[1] << endl; p = magic2(); p = magic3();

Memory leak The program of the previous slide is a classic example of a Memory Leak We allocate some memory with new, but then LOSE its address (by overwriting p) This makes this memory UNREACHABLE However, the memory is still allocated for our program... Do this enough, and your program will crash/slow down the computer (see eg Firefox!)

delete When we no longer need some memory we have allocated in the heap we can free it with the delete operator Syntax: delete p; where p is a pointer returned by new. int * p = new int; ; delete p; Syntax: delete [ ] p; where p is a pointer returned by new [ ] int * p = new int[5]; ; delete [ ] p;

Guess the output int *p1, *p2; //Note: not int *p1, p2; why? p1 = new int; p2 = new int; *p1 = 100; *p2 = 200; cout << *p1 << *p2 << endl;

Guess the output int *p1, *p2; //Note: not int *p1, p2; why? p1 = new int; p2 = new int; *p1 = 100; *p2 = 200; cout << *p1 << *p2 << endl; delete p1; p1 = p2; cout << *p1 << *p2 << endl;

Guess the output int *p1, *p2; //Note: not int *p1, p2; why? p1 = new int; p2 = new int; *p1 = 100; *p2 = 200; cout << *p1 << *p2 << endl; delete p1; p1 = p2; cout << *p1 << *p2 << endl; *p2 = 300; cout << *p1 << *p2 << endl; //What's missing?

Find the bug int *p1, *p2; //Note: not int *p1, p2; why? p1 = new int; p2 = new int; *p1 = 100; *p2 = 200; cout << *p1 << *p2 << endl; p1 = p2; cout << *p1 << *p2 << endl; *p1 = 300; cout << *p1 << *p2 << endl;

Find the bug int *p1, *p2, *p3; p1 = new int; p2 = new int; p3 = p1; *p1 = 100; *p2 = 200; cout << *p1 << *p2 << endl; delete p1; p1 = p2; cout << *p1 << *p2 << endl; *p1 = 300; cout << *p1 << *p2 << endl; delete p2;

Dangling pointers The last situation is a disaster waiting to happen (mild exaggeration) Problem: p3 is still pointing to the same area in memory as p1 But this area in memory has now been deleted! Though p1 has changed value, you may still (accidentally try to access it through p3)

Find the bug int *p1, *p2, *p3; p1 = new int; p2 = new int; p3 = p1; *p1 = 100; *p2 = 200; cout << *p1 << *p2 << endl; //Clean everything up delete p1; delete p2; delete p3;

No double-deletes! The previous program makes the dangling pointer problem worse Through p3, we try to delete the same area of memory twice! This is not allowed and will probably crash you program immediately

Heap operations summary Allocate memory with new T; //returns T* new T[ ]; //return T* Free memory with delete / delete [ ] Don't forget to delete what you allocate! Don't lose references to what you allocate! Don't keep pointing to what you deleted! No double deletes!

Back to Pointers/Arrays Warm-up exercise: write a function that checks if two positive int arrays are permutations of each other (they contain the same elements in perhaps different order) Task 1: define function prototype Task 2: algorithm? Task 3: program... (Spec: function should not change the two arrays)

One solution bool isperm(int a[ ], int b[ ], int size) //Assume a,b have size size, only positive ints { int i,j; for(i=0; i<size; i++){ for(j=0;j<size;j++){ if(a[i] == b[j]){ b[j] = -1; break; //Only exits the inner loop } } } for(i=0; i<size; i++){ if(b[i]!=-1) return false; } return true; }

Make a copy of an array Given an array b of size size, construct an array c of the same size and the same elements (a copy) Recall, c = b does not work for arrays

Array copy //Given int b[size] int *c = new int [size]; //Allocate memory for(int i=0; i<size; i++){ c[i] = b[i]; } //do something with c delete [ ] c; //Don't forget!

Array copy (old-fashioned) //Given int *b, int size int *c = new int [size]; //Allocate memory while(size--){ } *c++ = *b++; Why does this work?? Pointer arithmetic Note that this ruins the variable size This kind of copy is common in C code...

2-d Arrays C++ allows us to allocate 2-d arrays (or even higher dimensions) naturally: int a[5][5]; int i; a[2][3] = 7; //OK! for(i=0; i<5; i++) cout << a[i] << endl; //What does this mean?

2-d Arrays Recall the semantics of the [ ] operator Is applied to expression of type pointer to T Returns type T The expression a[2][3] can be read as (a[2])[3] == apply [3] to the expression a[2] a[2] is an int * The way to view 2-d arrays in C++ is as arrays of pointers

2-d Arrays (stack) int a[5][5]; int i; a[2][3] = 7; //OK! for(i=0; i<5; i++) cout << a[i] << endl; //What does this mean? This prints (in hex): 0x7fff97c34040 0x7fff97c34054 0x7fff97c34068 0x7fff97c3407c 0x7fff97c34090 Array is stored row-by-row by default...

Pointers to Pointers Pointer/Array equivalence is essential for 2-d Arrays Recall: in int a[5][5] the expression a[2] has type int * What type does a have? When [2] is applied to it we get int * [ ] removes one * Answer: int ** (!!)

Pointers to Pointers Recall: we can define pointers to any valid type, including pointer types int x = 2; int *p = &x; int **pp = &p; //What about int **pp = p? cout << **pp; //Output?

Pointers to Pointers int x = 2, y=3; int *p = &x; int **pp = &p; *pp = &y; //? cout << *p; //Output?

2-d Arrays on the heap We need ptrs to ptrs to dynamically allocate 2-d arrays int **a = new int[5][5]; a[2][3] = 17; //OK Unfortunately, the first line doesn't work! The second line is OK! Recall ptr-array equivalence

2-d Arrays on the heap A dynamic 2-d arrays is an arrays of arrays it is an array of pointers! Step 1: define a to have appropriate type int **a;

2-d Arrays on the heap Step 2: allocate space for the pointers that will hold each row int ** a = new int * [rows]; Recall how the new operator works (new Type [size]) This means: an array of size rows, each element of which has type (int *)

2-d Arrays on the heap Step 3: allocate each row individually int ** a = new int * [rows]; for (int i=0; i<rows; i++) a[i] = new int [columns]; Note: memory is not guaranteed to be allocated consecutively (as happens on stack)! When we are done, we need to delete all this...

2-d Arrays on the heap How to free a 2-d array allocated in this way Step 1: free each row Step 2: free arrays of row pointers for(int i=0; i<rows; i++) delete [ ] a[i]; delete [ ] a;

Irregular 2-d Arrays Create a triangular array of rows rows: Row r contains r+1 elements The numbers 0,1,...,r+1

Triangular array int ** a = new int * [rows]; for(int i=0; i<rows; i++){ } a[i] = new int [i+1]; for(int j=0; j<i+1; j++) a[i][j] = j;

Const pointers We have seen the const keyword const int x = 5; x = 6; //Compiler error! Its semantics are a little complicated when pointers are involved

Const pointers We have seen the const keyword const int x = 5; x = 6; //Compiler error! Which of these is an error? int i,j; const int *p = &i; p[0] = 2; p = &j;

Const pointers p[0] = 2 is an error The definition of p means it is pointing to a constant int To make p a constant pointer that always points to the same place use int * const p; More generally, semantics of * can be a little confusing with regards to precedence

Precedence of * The [ ] operator has higher priority int *a[4]; int (*a)[4]; int *(*a)[4];

Precedence of * The [ ] operator has higher priority int *a[4]; //Pointer to 4-int array (Type: int *) int (*a)[4]; //Array of 4 int ptrs (Type: int **) int *(*a)[4]; //Pointer to array of 4 int ptrs (Type: int ***)

Precedence of * Also, the () operator has higher priority Wait, is () an operator? int * f (bool); //f is a function that takes bool returns int int (* f) (bool); //f is pointer to function that...

Higher-order functions Usually associated with functional languages, can be used in C/C++ int add(int x, int y) { return x+y; } int sub(int x, int y) { return x-y; } int (*f)(int, int); f = add; cout << f(2,3) << endl; f = sub; cout << f(2,3) << endl;