C++ for Java Programmers

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

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

C++ for Java Programmers

CS201 Some Important Definitions

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Pointers, Dynamic Data, and Reference Types

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Dynamic Allocation in C

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Dynamic Memory Allocation

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

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

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.

CSC 1600 Memory Layout for Unix Processes"

Dynamic Allocation in C

Intermediate Programming, Spring 2017*

Object-Oriented Programming

Chapter 17 vector and Free Store. Bjarne Stroustrup


CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Homework #3 CS2255 Fall 2012

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

o Code, executable, and process o Main memory vs. virtual memory

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Short Notes of CS201

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CS201 - Introduction to Programming Glossary By

Pointers II. Class 31

Dynamic Memory: Alignment and Fragmentation

What have we learned about when we learned about function parameters? 1-1

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

Class Information ANNOUCEMENTS

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

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

Vector and Free Store (Pointers and Memory Allocation)

Object Oriented Software Design II

377 Student Guide to C++

CS107, Lecture 9 C Generics Function Pointers

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Pointers in C/C++ 1 Memory Addresses 2

Dynamic Memory Management

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

CS61C : Machine Structures

Pointers. Developed By Ms. K.M.Sanghavi

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

Common Misunderstandings from Exam 1 Material

DAY 3. CS3600, Northeastern University. Alan Mislove

Dynamic Memory Management! Goals of this Lecture!

Launchpad Lecture -10

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

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

CS 230 Programming Languages

Chapter 17 vector and Free Store

Compiler Construction

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Lecture Notes on Memory Layout

Lecture Notes on Memory Management

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Advanced Programming & C++ Language

Heap Arrays and Linked Lists. Steven R. Bagley

Chapter 9. Pointers and Dynamic Arrays

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):

Lecture Notes on Memory Management

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Object-Oriented Programming for Scientific Computing

Arrays and Pointers. Overview. Arrays Introducing Pointers C-Style Character Strings Multidimensioned Arrays

Pointers. Memory. void foo() { }//return

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

Pointers and Memory 1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

ALL ABOUT POINTERS C/C++ POINTERS

Lecture 2, September 4

Implementing Abstractions

Data Structures and Algorithms for Engineers

ECE 15B COMPUTER ORGANIZATION

Pointers. Variable Declaration. Chapter 10

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

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

Data Structures and Algorithms for Engineers

Memory (Stack and Heap)

Data Structures Lecture 2 : Part 1: Programming Methodologies Part 2: Memory Management

Chapter 1: Object-Oriented Programming Using C++

Heap Arrays. Steven R. Bagley

Pointers. Reference operator (&) ted = &andy;

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

CS201- Introduction to Programming Current Quizzes

Data Structure Series

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Lecture 8 Dynamic Memory Allocation

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Memory and C++ Pointers

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Run-time Environments - 3

Transcription:

Lecture 6 More pointing action Yesterday we considered: Pointer Assignment Dereferencing Pointers to Pointers to Pointers Pointers and Array Pointer Arithmetic 2 Todays Lecture What do we know 3 And now we are going to look at: The Stack and the Heap Dynamic Memory Allocation New and Delete More Pointer Pitfalls Putting it all together I discovered the transition button in powerpoint and got carried away. 4 We have learnt how to use the dereferencing and referencing operators to manipulate pointers and the data to which they point. We looked at how we can use pointer notation as alternative for manipulating arrays. We saw how pointers are the mechanism used by which C++ processes command line arguments. pointers are useful Dynamic Memory 5 Consider the strlen exercise from the last lab sheet. We can solve this problem without using arrays at all int strlen(const *str) { int i; while (*str++) i++; return i; } So all in all we have seen that pointers can be handy things to have in your coding knowledge. 6 But these roles are not the main reasons that pointers are part of the C++ language. The major role of pointers is the creation of dynamic structures. Particularly useful when you don t know beforehand exactly how much memory you need. Notably its through dynamic memory that the Standard Template Library (STL) can provide its functionality

SCOPE again Not so for Dynamic Structures Almost all the variables, structures and objects we have used so far in out programs have generally been local objects. Dynamic objects are different in that they come in to memory due to direct memory allocation requests from the program. They come into existence when their definition is executed. They die when the block of code in which they live comes to and end. The dynamic objects continue to exist until the program returns their memory. But again it needs a direct command to do this. The same fundamental rules apply to global variables too they are created when defined, and destroyed when the program ends. The lifetime of a dynamic object is independent of its defining scope. 7 8 Different Memory Quick mention of C-style The memory comes for a dynamic structure comes from the free store. This is controlled by the OS, and is memory that can be broken up and allocated to a program as needed. Principal C++ way of acquiring this memory is using the new command. Unless you have coded in C you might not appreciate the simplicity of new and delete. In C programming you use malloc(), calloc(), realloc(), and free() to dynamically allocate memory. OS such as windows can complicate things even further, offering its own functions. 9 Deallocating it is handled by the delete command. 0 Although these things aren t amazingly difficult, C++ s new and delete can save you a lot of headaches. Memory Segments Brain.ico Calc3.ico Memory, its properties and organization is a very simple idea to comprehend. One should keep in mind the simple idea that a computer is little more than a over-grown calculator with better I/O Memory is nothing more than a linear set of bytes. The computer needs a method to access all the memory on the system and it does this through addressing: giving each byte in memory an identity. 2 Memory within a computer typically is made of segments - These segments are simply regions of memory with a starting and ending address, assigned attributes. A program cannot write to a segment that does not have the write permission attribute assigned to it. To do with common problems like General Protection Faults, Unrecoverable Application Errors, and such like. 2

Segments The Stack Drawers.ico When a program runs in memory, it has the following memory organization: CODE DATA BSS STACK HEAP In general, a is a single ended data structure with a first in, last out data ordering. The is useful for storing context - A function simply pushes all its local variables onto the when it enters, and pops them off when its done. Where the statements themselves are stored This is where the programs global variables are. The home of all the local variables EVERYTHING ELSE! Its complete context is nicely cleaned up afterwards. This is what happens with local variables. 3 4 The Heap The New operator The is basically all the rest of memory the program has. Programs use the to hold data that must exist after a function call return. The holds values that have lifetimes more than local variables but less than global variables. The operator new has 3 forms. Simplest is a memory allocation request for a single object. This request expects the name of a type as the right operand. To achieve this, the uses memory allocation. In this technique, all the memory in the is managed by a pair of algorithms which make reservations. If there is sufficient unallocated free store memory, the operation returns a pointer to a memory location for that type. 5 6 The data with no name Finally the location of this memory is assigned to our pointer char *cptr; cptr = new char; first we define a char* object Allocate some free memory of type char Whatever type we are creating the specific amount of memory will be set up. Once a pointer has been successfully set with a location returned by a new request, the type at the location can be used like any other object of that type. cptr char In particular it can be evaluated and manipulated. 7 8 However, note that this object has no name. 3

What if we lose it So variable names are fixed but our pointer is no constant. We might change it by accident. So how do we get access to our nameless data again char* cptr; cptr = new char; cin >> *cptr; We dereference the pointer and so access the dynamic memory space we have been allocated We can t. It is gone. adios. forever. wave goodbye Even worse now we can t delete it and its just going to hang around, clogging up memory until we do something drastic. cptr J 9 20 Second form of new A second form of the new operator not only requests a single dynamic object, but initialises it too. The initialiser values are just specified within parentheses, using commas to separate any initialisers. char* mike; int* nilgun; mike = new char( A ); nilgun = new int(); Here we initialise the nameless data. So in general form: mike A pointer = new type(initialisers); nil 2 22 Third form of new The third form permits multiple dynamic objects to be requested in a single operation. float* numbers; numbers = new float[4]; This is like creating an array of objects in dynamic memory. Here we initialise the nameless data. 23 The form expects a type name along with a subscripted expression indicating the number of requested objects of that type. If there s enough contiguous and unallocated free memory a pointer is returned. This contains the memory address of the space we have been given. 24 numbers 4

The Point of pointers So again how is this any better than using an array created on the Why not just use a normal local variable. The point is that although we used a constant expression in our request for multiple dynamic objects we didn t have to. cout << Go on. pick an array size << flush; int listsize; cin >> listsize; int* values = new int[listsize]; Here we combine two statements. We use a variable with the new command 25 You can use variables, or expressions. That means that we can create structures like arrays in dynamic memory, and determine their size during runtime. We don t have to know the size of the array. 26 values Yin and Yang - Delete 27 Everything that you create with the new command you MUST delete too. This often turns out to be more responsibility than some budding programmers can cope with. Using the delete operation we can free up that dynamic memory in the. But we need that pointer to tell it which memory location to free up. 28 char* mike; int* nil; mike = new char( A ); nilgun = new int(); delete mike; delete nilgun; mike nil A Take care with your pointers Dangling Pointers 29 What happens to the pointers we used Nothing the value of the pointer just becomes undefined. It is good form because of this to set the pointer to null after you have used it in a delete statement; delete mike; mike = null; delete nilgun; nilgun = null; 30 You have to be really careful of this as a programmer, especially if your code contains multiple pointers to the same object. char *ptr; char *ptr2; ptr = new char( C ); ptr2 = ptr; delete ptr; cout << *ptr2; ptr ptr2 C 5