Managing Memory (and low level Data Structures) Lectures 22, 23 Hartmut Kaiser

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Programming Languages and Paradigms The C Programming Language.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C Programming - Lecture 5
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Chapter 10.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Pointer Data Type and Pointer Variables
Lecture 11 vector and Free Store Bjarne Stroustrup
CS352-Week 2. Topics Heap allocation References Pointers.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 17 vector and Free Store Hartmut Kaiser
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Defining New Types Lecture 21 Hartmut Kaiser
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Looping and Counting Lecture 3 Hartmut Kaiser
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Review 1 List Data Structure List operations List Implementation Array Linked List.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 10 Managing Memory and Low-Level Data Structures.
Dynamic Allocation in C
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 18 Vectors and Arrays
Motivation and Overview
Arrays in C.
Lecture 6 C++ Programming
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 17 vector and Free Store
Chapter 15 Pointers, Dynamic Data, and Reference Types
Classes and Objects.
Dynamic Memory.
Submitted By : Veenu Saini Lecturer (IT)
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Managing Memory (and low level Data Structures) Lectures 22, 23 Hartmut Kaiser

Programming Principle of the Day Avoid Premature Optimization  Don’t even think about optimization unless your code is working, but slower than you want. Only then should you start thinking about optimizing, and then only with the aid of empirical data.  "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" - Donald Knuth. 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 2

Low Level Data Structures We were using the standard library data structures – containers  How are these built?  Low level language facilities and data structures  Closer to computer hardware (in semantics and abstraction level)  Why do we need to know how are these built?  Useful techniques, applicable in other contexts  More dangerous, require solid understanding  Sometimes absolute performance matters 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 3

Pointers and Arrays Array is a kind of container, however it’s less powerful and more dangerous Pointers are kind of random access iterators for accessing elements of arrays Pointers and Arrays are the most primitive data structures available in C/C++  Closely connected concepts, inseparable in real world usage 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 4

Pointers A pointer is a value representing the address of an object  Every distinct object has a distinct address denoting the place in memory it lives  If it’s possible to access an object it’s possible to retrieve its address For instance: x // if ‘x’ is an object &x // then ‘&x’ is the address of this object p // if ‘p’ is the address of an object *p // then ‘*p’ is the object itself 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 5

Pointers The ‘&’ is the address-of operator  Distinct from defining reference types! The ‘*’ is the dereference operator  Same as for any other iterator as well If ‘p’ contains the address of ‘x’ we say that ‘ the pointer p points to x’ Pointers are built-in data types which need to be initialized in order to be meaningful 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 6 p p x x

Pointers Initialize to zero means ‘point to no object’  Null pointer (special value, as no object has this address)  C++11: nullptr Pointers have types!  The address of an object of type T is ‘ pointer to T ’  Written as: T* For instance: int x; // object of type int int *p; // pointer to an int, *p has type int int* p; // pointer to an int, p has type int* 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 7

Pointers A small (but full) example: int main() { int x = 5; // p points to x int* p = &x; cout << "x = " << x << endl; // change the value of x through p *p = 6; cout << "x = " << x << endl; return 0; } 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 8 p p x: 5 p p x: 6

Pointers Think of pointers to be iterators They point to the single object stored in a ‘virtual’ (non- existent) container 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 9

Arrays Part of the language rather than standard library  Hold sequence of elements of the same type  Size must be known at compile time  No member functions, no embedded typedefs  i.e. no size_type member, use std::size_t instead 3 dimensional point: double coords[3]; // or std::size_t const ndim = 3; double coords[ndim]; 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 10

Arrays Fundamental relationship between arrays and pointers  Name of the array is interpreted as a pointer to its first element: *coords = 1.5; // set first element in coords to 1.5 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 11

Pointer Arithmetic Pointer is a random-access iterator  If ‘p’ points to the mth element of an array, then  ‘p+n’ points to the (m+n)th element of the array  ‘p-n’ points to the (m-n)th element of the array Further, as first element of array has number ‘0’  coords+1 points to the second, and  coords+2 points to the third element  coords+3 points to the first element after the last Possible to use standard algorithms with arrays: vector v; copy(coords, coords + ndim, back_inserter(v)); 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 12

Pointer Arithmetic Possible to initialize containers from arrays: vector v(coords, coords + ndim); More generally, wherever we used v.begin() and v.end(), we can use a and a+n (a: array, n: size) If ‘p’ and ‘q’ are pointers  Then p-q is the distance of the two pointers, which is the number of elements in between  Further (p – q) + q == p 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 13

Indexing If ‘p’ points to the mth element of an array, then p[n] is the (m+n)th element, not its address Consequently, if ’a’ is an array, and ‘n’ an integer, then a[n] refers to the nth element inside the array ‘a’. More formally, if ‘p’ is a pointer, and ‘n’ an integer, then p[n] is equivalent to *(p+n) In C++ indexing is not a property of arrays, but a corollary to the properties of pointers and arrays and the fact that pointers are random access iterators 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 14

Array Initialization Historically, arrays can be initialized easily: int const month_lengths[] = { // we will deal elsewhere with leap years 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; No size specified, it’s automatically calculated If size is specified, missing elements are set to zero (value- initialized) C++11 allows the same syntax for containers std::vector const month_lengths = { // we will deal elsewhere with leap years 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 15

String Literals Revisited String literals are character arrays with a trailing zero byte These are equivalent: char const hello[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; "Hello" Null character is appended to be able to locate the end of the literal Library has special functions dealing with ‘C’ strings (string literals) 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 16

String Literals Revisited Find the length of a string literal (‘C’ string): strlen // Example implementation of standard-library function size_t strlen(char const* p) { size_t size = 0; while (*p++ != '\0') ++size; return size; } Counting bytes (characters) excluding the null character 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 17

String Literals Revisited Variable hello and literal “Hello” are equivalent: string s(hello); string s("Hello");  All will construct a string instance ‘s’ holding “Hello” Pointers are iterators: string s(hello, hello + strlen(hello)); 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 18

Pointers and References Think of a reference as an automatically dereferenced pointer  Or as “an alternative name for an object”  A reference must be initialized  The value of a reference cannot be changed after initialization int x = 7; int y = 8; int* p = &x; *p = 9; p = &y; // ok int& r = x; r = 10; r = &y; // error (and so is all other attempts to // change what r refers to) 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 19

Arrays of Character Pointers String literal is convenient way of writing address of first character of a null terminated string Arrays can be initialized conveniently Show how to initialize an array of character pointers from sequence of string literals Grading (again *sigh*): 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 20 If the grade is at least Then the letter grade isA+AA-B+BB-C+CC-DF

Arrays of Character Pointers string letter_grade(double grade) { // range posts for numeric grades static double const numbers[] = { 97, 94, 90, 87, 84, 80, 77, 74, 70, 60, 0 }; // names for the letter grades static char const* const letters[] = { "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F" }; // compute the number of grades given the size of the array // and the size of a single element static size_t const ngrades = sizeof(numbers)/sizeof(numbers[0]); // given a numeric grade, find and return the associated letter grade for (size_t i = 0; i < ngrades; ++i) { if (grade >= numbers[i]) return letters[i]; } return "?\?\?"; } 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 21

Arguments to main() Command line arguments are optionally passed to main  Alternative prototype for main(): int main(int argc, char** argv);  argc: number of arguments  argv: pointer to an array of character pointers, one argument each  At least one argument: the name of the executable itself, thus argc >= 1 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 22

Arguments to main() Let’s assume our executable is called ‘say’ Invoking it as say Hello, world Should print: Hello, world 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 23 argv char ** argc: 3 int argv[0] argv[1] argv[2] char * s s a a y y \0 char H H e e l l l l o o,, \0 w w o o r r l l d d

Arguments to main() int main(int argc, char** argv) { // if there are arguments, write them if (argc > 1) { int i; // declare i outside the for because we need // it after the loop finishes // write all but the last entry and a space for (i = 1; i < argc-1; ++i) cout << argv[i] << " "; // argv[i] is a char* cout << argv[i] << endl; // write the last entry // but not a space } return 0; } 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 24

Multiple Input Files Print the content of all files given on command line to console: int main(int argc, char** argv) { int fail_count = 0; // for each file in the input list for (int i = 1; i < argc; ++i) { ifstream in(argv[i]); // if it exists, write its contents, otherwise // generate an error message if (in) { string s; while (getline(in, s)) cout << s << endl; } else { cerr << "cannot open file " << argv[i] << endl; ++fail_count; } return fail_count; } 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 25

The Computer’s Memory As a program sees it  Local variables “live on the stack”  Global variables are “static data”  The executable code are in “the code section” 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 26

Three Kinds of Memory Management Automatic memory management  Local variables  Allocated at the point of the definition  De-allocated at the end of the surrounding scope  Memory becomes invalid after that point: // this function deliberately yields an invalid pointer. // it is intended as a negative example—don't do this! int* invalid_pointer() { int x; return &x; // instant disaster! } 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 27

Three Kinds of Memory Management Static memory management  Memory allocated once  Either at program startup (global variables)  Or when first encountered (function-static variables)  De-allocated at program termination: // This function is completely legitimate. int* pointer_to_static() { static int x; return &x; }  Always returns pointer to same object 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 28

Three Kinds of Memory Management Dynamic memory management  Allocate an instance of T with ‘new T’  De-allocate an existing instance pointed to by ‘p’ with ‘delete p’: int* p = new int(42); // allocate int, initialize to 42 ++*p; // *p is now 43, same as ++(*p) delete p; // delete int pointed to by p Another example: int* pointer_to_dynamic() { return new int(0); } 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 29

Allocating an Array Arrays of type T are dynamically allocated using ‘new T[n]’, where n is the number of allocated elements De-allocation of an array pointed to by p is done using ‘delete [] p’ ‘n’ can be zero! (Why?) T* p = new T[n]; vector v(p, p + n); delete[] p; Only way to create a dynamically sized array (remember, static array has to have size known at compile time) 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 30

A Problem: Memory Leak double* calc(int result_size, int max) { double* p = new double[max]; // allocate another max doubles // i.e., get max doubles from the free store double* result = new double[result_size]; // … use p to calculate results to be put in result … return result; } double* r = calc(200,100); // oops! We “forgot” to give the memory // allocated for p back to the free store Lack of de-allocation (usually called "memory leaks") can be a serious problem in real-world programs A program that must run for a long time can't afford any memory leaks 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 31

A Problem: Memory Leak double* calc(int result_size, int max) { int* p = new double[max]; // allocate max doubles // i.e., get max doubles from // the free store double* result = new double[result_size]; // … use p to calculate results to be put in result … delete[] p; // de-allocate (free) that array // i.e., give the array back to the // free store return result; } double* r = calc(200,100); // use r delete [] r; // easy to forget 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 32

Memory Leaks A program that needs to run "forever" can't afford any memory leaks  An operating system is an example of a program that "runs forever" If a function leaks 8 bytes every time it is called, how many days can it run before it has leaked/lost a megabyte?  Trick question: not enough data to answer, but about 130,000 calls All memory is returned to the system at the end of the program  If you run using an operating system (Windows, Unix, whatever) Program that runs to completion with predictable memory usage may leak without causing problems  i.e., memory leaks aren't "good/bad" but they can be a problem in specific circumstances 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 33

Memory Leaks Another way to get a memory leak void f() { double* p = new double[27]; // … p = new double[42]; // … delete[] p; } // 1st array (of 27 doubles) leaked 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 34 p: 2 nd value 1 st value

Memory Leaks How do we systematically and simply avoid memory leaks?  Don't mess directly with new and delete  Use vector, etc.  Or use a garbage collector  A garbage collector is a program that keeps track of all of your allocations and returns unused free-store allocated memory to the free store (not covered in this course; see  Unfortunately, even a garbage collector doesn’t prevent all leaks  Use RAII, see next lecture 4/14/2015, Lectures 22, 23 CSC 1254, Spring 2015, Managing Memory 35