dynamically allocated memory char* x = new char; int* x = new int[n]; ???...?

Similar documents
a data type is Types

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CS201 Some Important Definitions

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

pointers + memory double x; string a; int x; main overhead int y; main overhead

377 Student Guide to C++

CS201- Introduction to Programming Current Quizzes

Pointers, Dynamic Data, and Reference Types

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CS 376b Computer Vision

CS250 Final Review Questions

Pointers. Developed By Ms. K.M.Sanghavi

CS250 Final Review Questions

CSE 303: Concepts and Tools for Software Development

CS250 Final Review Questions

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Classes in C++98 and C++11

Lecture 2, September 4

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

2 2

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Common Misunderstandings from Exam 1 Material

CSE 333 Midterm Exam Sample Solution 5/10/13

CPSC 427: Object-Oriented Programming

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CSC1322 Object-Oriented Programming Concepts

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

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

Memory and C++ Pointers

Lecture 15a Persistent Memory & Shared Pointers

Programming Abstractions

Scope. Scope is such an important thing that we ll review what we know about scope now:

CSE 333 Midterm Exam 5/10/13

WHAT POINTERS REALLY ARE

CPSC 427: Object-Oriented Programming

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

05-01 Discussion Notes

Object Reference and Memory Allocation. Questions:

Class CSE F. *slides are from CSE S at SKKU & at MIT

COMP 2355 Introduction to Systems Programming

C Introduction. Comparison w/ Java, Memory Model, and Pointers

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

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS105 C++ Lecture 7. More on Classes, Inheritance

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

1. Write the number of the definition on the right next to the term it defines. (a) copy 4

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

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

Written by John Bell for CS 342, Spring 2018

377 Student Guide to C++

Pointers II. Class 31

CS 251 Intermediate Programming Methods and Classes

CSE 333 Midterm Exam July 24, Name UW ID#

Fall 2018 Discussion 2: September 3, 2018

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

AIMS Embedded Systems Programming MT 2017

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)

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Programming Abstractions

C:\Temp\Templates. Download This PDF From The Web Site

RAII and Smart Pointers. Ali Malik

G52CPP C++ Programming Lecture 13

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

CSCE 110 PROGRAMMING FUNDAMENTALS

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4


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

Arrays and Linked Lists

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

III. Classes (Chap. 3)

Financial computing with C++

LECTURE 03 LINKED LIST

Pointers and Terminal Control

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

CSE 333 Midterm Exam 7/27/15 Sample Solution

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

内存管理. Memory management

OBJECT ORIENTED PROGRAMMING

ALL ABOUT POINTERS C/C++ POINTERS

CSCE 206: Structured Programming in C++

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Object-Oriented Programming, Iouliia Skliarova

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Exception Safe Coding

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

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:

CS 251 Intermediate Programming Methods and More

Function Overloading

Transcription:

dynamically allocated memory char* x = new char; yields a memory address 1. allocates memory for a char 2. declares a pointer to a char 3. sets pointer to memory address x pointer? memory (1 byte) int* x = new int[n]; yields a memory address 1. allocates memory for int array 2. declares a pointer to an integer 3. sets pointer to memory address x pointer???...? memory (4 * n bytes) CS31 Fall 2014 Discussion 8: dynamic memory, struct, classes

stack vs. heap while typical declarations allocate memory on the stack, using new allocates memory in the heap stack heap scope local global allocate one thing int x; int* px = new int; allocate array int x[5]; int* pax = &x[0]; int* pax = new int[5]; use pax[3] = 12; pax[3] = 12; cleanup one thing do nothing :D delete px; cleanup array do nothing :D delete [] pax; code snippet char s[12]; strcpy(s, hello mississippi ); while (s!= 0) { if (*s == *(s+1)) { *s++; cout << s; both approaches print helmo mististipqi char* s = new char[12]; strcpy(s, hello mississippi ); while (s!= 0) { if (*s == *(s+1)) { *s++; cout << s; delete s;

allocating memory in functions dynamic allocation char* s = new char[n]; delete [] word; local allocation char str[n]; char* s = &str[0]; stack heap stack heap please note that n should also appear on the stack in this example, but it will be omitted due to lack of foresight on my part. sorry -- Brian

allocating memory in functions dynamic allocation char* s = new char[n]; delete [] word; local allocation char str[n]; char* s = &str[0]; stack heap stack heap char *s char* strs fxn1() fxn1()

allocating memory in functions dynamic allocation char* s = new char[n]; delete [] word; local allocation char str[n]; char* s = &str[0]; char* stack s heap stack heap char* s char* s2 h e y \0 char* s2 h e y \0 fxn2() fxn2() char *s char* strs h e y \0 fxn1() fxn1() h e y \0

allocating memory in functions dynamic allocation char* s = new char[n]; delete [] word; local allocation char str[n]; char* s = &str[0]; stack heap stack heap char *s char* strs h e y \0 fxn1() fxn1() h e y \0

allocating memory in functions dynamic allocation char* s = new char[n]; delete [] word; local allocation char str[n]; char* s = &str[0]; stack heap stack heap prints hey prints random garbage char* strs h e y \0 h e y \0

memory management practice (new/delete) stack memory is automatic, heap memory is not managed by the operating system memory leak occurs when memory allocated is not de-allocated before the program completes code compiles/runs? leak? int *s1, *s2; int s3[4]; s1 = new int[4]; s2 = new int[6]; delete [] s1; int *s1, *s2; int s3[4]; s1 = new int[4]; s2 = &s3[0]; delete [] s1; delete [] s2; yep int *s1, *s2; int s3[4]; s1 = new int[4]; s2 = &s3[0]; delete [] s1; nope int *s1, *s2; yep, 24 bytes int s3[4]; s1 = new int[4]; s2 = new int[6]; delete [] s1; delete [] s2; nope int *s1, *s2; int s3[4]; s1 = new int[4]; s2 = new int[6]; s1 = s2; delete [] s1; yep int *s1, *s2; yep, 16 bytes int s3[4]; s1 = new int[4]; s2 = new int[6]; delete [] s1; s1 = s2; delete []s2; tip: for every new, there should be an associated delete

A bit more about delete Q: Can multiple objects be deleted on the same line? nope each call to delete must be on its own line, so given pointers a, b, c (e.g. int *a, *b, *c; ): BREAKS delete a, b, c; RUNS delete a; delete b; delete c; Q: How does delete know how much memory to give back to the operating system? When you allocate memory on the heap, your allocator will keep track of how much memory you have allocated. This is usually stored in a "head" segment just before the memory that you get allocated. That way when it's time to free the memory, the deallocator knows exactly how much memory to free. http://stackoverflow.com/questions/703691/how-does-delete-know-its-an-array Q: Let s say we re given int* A = new int[10]; and int* B = new int;. What happens if you do delete [ ] B;? What if you do delete A; http://stackoverflow.com/questions/197675/how-does-delete-know-the-size-of-the-operand-array

how much is it leaking? (problem code) int sum(int* x, int n) { int* part_sum = new int[n]; part_sum[0] = x[0]; for (int k = 1; k < n; k++) { part_sum[k] = part_sum[k-1] + x[l]; return part_sum[n - 1]; 4 * p * n bytes fix: add delete [ ] src; before src = dst; bool lift(int dst, int now) { int* start = new int; *start = now; if (*start == dst) { return true; else { while (*start < dst) { *start++; delete start; return true; 4 * n bytes fix: add delete [ ] part_sum; int* pow(int* x, int p, int n) { int* dst = new int[n]; int* src = x; for (int j = 1; j < p; j++) { for (int k = 0; k < n; k++) { dst[k] = src[k] * x[k]; src = dst; dst = new int[n]; return src; 4 bytes fix: add delete start; in the first if cond p must be larger than 2

how much is it leaking? (fixed code) int sum(int* x, int n) { int* part_sum = new int[n]; part_sum[0] = x[0]; for (int k = 1; k < n; k++) { part_sum[k] = part_sum[k-1] + x[l]; int total = part_sum[n - 1]; delete [] part_sum; return total; 4 * p * n bytes fix: add delete [ ] src; before src = dst; bool lift(int dst, int now) { int* start = new int; *start = now; if (*start == dst) { delete start; return true; else { while (*start < dst) { *start++; delete start; return true; 4 * n bytes fix: add delete [ ] part_sum; int* pow(int* x, int p, int n) { int* dst = new int[n]; int* src = new int[n]; for (int k = 0; k < n; k++) { src[k] = x[k]; p must be larger than 2 for (int j = 1; j < p; j++) { for (int k = 0; k < n; k++) { dst[k] = src[k] * x[k]; delete [] src; src = dst; dst = new int[n]; delete [] dst; return src; 4 bytes fix: add delete start; in the first if cond

structures struct is a group of data grouped under a single name. The data does not have to be of the same type struct student { int sid; char* name; int year; string field; int age; double wage; double gpa; ; struct prof { bool tenure; string name; string field; double wage; ; student brian; prof david; // set some things brian.sid = 123456789; david.name = David ; // see some things cout << brian.name << endl; cout << david.field << endl; if (david.tenure) { david.wage++; bad call, segfaults since name isn t set

classes class is pretty much the same as a struct, but slightly more private class student { public: int sid; char* int sid; name; int char* year; name; string int year; field; int string age; field; double int age; wage; double gpa; wage; ; double gpa; ; class prof { class bool prof tenure; { public: string name; string bool tenure; field; double string wage; name; ; string field; double wage; ; student brian; prof david; // set some things brian.sid = 123456789; david.name = David ; // see some things cout << brian.name << endl; cout << david.field << endl; if (david.tenure) { david.wage++; bad call, segfaults since name isn t set in a struct, all functions and variables default to public access. In a class, they default to private

member functions member functions are functions specific to a class (e.g. get and set functions) class prof { private: bool tenure; string name; string field; double wage; prof david; // set some things david.set_name( David ); david.set_tenure(true); public: void talk() { cout << Did you read ; cout << the spec? ; // accessor fxns string get_name() { return name; void set_name(string s) { name = s;... ; // see some things cout << Prof. << david.get_name(); cout << says, \ << david.talk(); cout << / << endl; if (david.get_tenure()) { david.set_wage(david.get_wage() + 1); cout << Field: << david.get_field();

accessor functions style a minimalist, cleaner style for names for object variables and accessor functions class prof { private: bool _tenure; string _name; string _field; double _wage; public: void talk() { cout << Did you read ; cout << the spec? ; // accessor fxns string name() { return _name; void name(string s) { _name = s;... ; prof david; // set some things david.name( David ); david.tenure(true); // see some things cout << Prof. << david.name(); cout << says, \ << david.talk(); cout << / << endl; if (david.tenure()) { david.wage(david.wage() + 1); cout << Field: << david.field(); - private variables preceded by _ - get/set functions overloaded with same name

constructors constructors are used to create instances of an object class prof { private: bool _tenure; string _name; string _field; double _wage; public: prof() { //default _tenure = false; _name = Prof ; _field = CS ; _wage = 200000;... prof(bool t, string n, ; string f, double w) { _tenure = t; _name = n; _field = f; _wage = w;... ; prof bill; prof david(true, David, CS, 250000); // set some things bill.name( Bill ); bill.tenure(true); // see some things cout << Prof. << bill.name(); cout << Prof. << david.name(); if (david.tenure()) { david.wage(david.wage() + 1); - if not defined, a default constructor is created

destructors the destructor is called when an object is deleted and the memory is returned to the operating system class prof { private: bool _tenure; char* _name; string _field; double _wage; prof bill; prof david(true, David, CS, 250000); public: ~prof() { //destruct delete [] _name; prof(bool t, string n, string f, double w) { //construct _tenure = t; *_name = new char[f.size()]; strcpy(_name, f.c_str()); _field = f; _wage = w;... ; // set some things bill.name( Bill ); bill.tenure(true); // see some things cout << Prof. << bill.name(); cout << Prof. << david.name(); if (david.tenure()) { david.wage(david.wage() + 1); delete david; delete bill; for this to work, the default constructor (no arguments) must initialize name to a char array - destructor only needs to remove un-managed memory on the heap

arrays of object pointers class dis { private: student** _students; char* _name; int _nstudents; public: dis(string name, int n); ~dis(); mean_gpa(); ; dis::dis(string name, int n) { // name _name = new char[name.size()]; strcpy(_name, name.c_str()); int _nstudents = n; array of student pointers // create students _students = new student*[n]; for (int i = 0; i < n; i++) { _students[i] = new student; double dis::mean_gpa() { double sum_gpa = 0.0; for (int i = 0; i < _nstudents; i++) { // sum_gpa += (*_students[i]).gpa(); sum_gpa += _students[i]->gpa(); return sum_gpa /_nstudents; dis::~dis() { delete [] _name; for (int i = 0; i < _nstudents; i++) { delete _students[i]; delete [] _students; dis* 1G = new dis( 1G, 10); dis* 1A = new dis( 1A, 8); if (1G.mean_gpa() < 1A.mean_gpa()) { cout << Brian gets fired << endl; else { cout << Liuli gets fired << endl; delete 1G; delete 1A;

project 7 tips - check out the warmup online. it s really good practice and short and easy to do. Most of the code is already written for you so you don t have to write much to have something up and running, which can be a daunting task when starting out with classes and objects and such things - download the example and play it (yay games) - do the 5 experiments