Launchpad Lecture -10

Similar documents
Dynamic Memory Allocation

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Pointers, Dynamic Data, and Reference Types

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Exam 3 Chapters 7 & 9

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

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

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

Dynamic Allocation in C

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

An Introduction to C++

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

And Even More and More C++ Fundamentals of Computer Science

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

Special Member Functions

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Dynamic Allocation in C

Homework #3 CS2255 Fall 2012

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

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

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

CS201 Some Important Definitions

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CS201- Introduction to Programming Current Quizzes

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


Implementing Abstractions

Short Notes of CS201

Run-time Environments - 3

Computer Programming

CS201 - Introduction to Programming Glossary By

Object-Oriented Programming

Intermediate Programming, Spring 2017*

UNIVERSITY OF SWAZILAND

CSC1322 Object-Oriented Programming Concepts

Come and join us at WebLyceum

C Pointers. 6th April 2017 Giulio Picierro

C++ Programming Lecture 4 Software Engineering Group

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

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

PIC10B/1 Winter 2014 Exam I Study Guide

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

C++ for Java Programmers

Pointers and Dynamic Memory Allocation

CS304 Object Oriented Programming Final Term

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Class Information ANNOUCEMENTS

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Multidimensional Arrays in C++: Trying to get it right

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

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

Programming, numerics and optimization

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

CPSC 427: Object-Oriented Programming

CS201 Latest Solved MCQs

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

CPSC 427: Object-Oriented Programming

Variables, Memory and Pointers

Variation of Pointers

CS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000

ADT Sorted List Operations

Lecture 5. Function Pointers

Introduction to Programming Using Java (98-388)

Computer Programming

Object Oriented Programming COP3330 / CGS5409

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

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

CS24 Week 3 Lecture 1

Object-Oriented Programming for Scientific Computing

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

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

[0569] p 0318 garbage

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

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

Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array

CS11 Intro C++ Spring 2018 Lecture 5

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Object-Oriented Principles and Practice / C++

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

C++ 8. Constructors and Destructors

Financial computing with C++

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

Lecture 2: C Programm

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

PHY4321 Summary Notes

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

a data type is Types

Initializing and Finalizing Objects

Name: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Common Misunderstandings from Exam 1 Material

G52CPP C++ Programming Lecture 13

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

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

Programming in C - Part 2

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Programming Languages

Transcription:

Saturday, 24 September Launchpad Lecture -10 Dynamic Allocation, Object Oriented Programming-1 Prateek Narang

2 Recap 1. How to define pointers? 2. Address of Operator? 3. Dereference operator? 4. Arithmetic Operators on pointers? 5. Arrays & Pointers 6. Reference Variables 7. Pass by reference 8. Returning pointers or References from functions

3 Monday, June 9, 2014 Address typecasting

4 Saturday, September 24, Dynamic Memory Allocation!

5 Saturday, September 24, Allocating Memory There are two ways that memory gets allocated for data storage: Compile Time (or static) Allocation Memory for named variables is allocated by the compiler Exact size and type of storage must be known at compile time For standard array declarations, this is why the size has to be constant Dynamic Memory Allocation Memory allocated "on the fly" during run time dynamically allocated space usually placed in a program segment known as the heap or the free store Exact amount of space or number of items does not have to be known by the compiler in advance. For dynamic memory allocation, pointers are crucial

6 Saturday, September 24, Dynamic Memory Allocation We can dynamically allocate space while the program is running but we cannot create new variable names on the fly For this reason, dynamic allocation requires two steps 1. Creating the dynamic space 2. Storing its address in a pointer To dynamically allocate memory in C++, we use new operator De-allocation: De-allocation is the clean-up of space being used by variable

7 Saturday, September 24, De-allocation De-allocation is the clean up of space being used by variables or other data storage Compile time variable are automatically deallocated based on their know scope It is the programmer s job to deallocate dynamically created memory To de-allocate dynamic memory we use delete operator

8 Saturday, September 24, new operator To allocate space dynamically, use the unary operator new, followed by the type being allocated. new int; // dynamically allocates an int new double; // dynamically allocates a double If creating an array dynamically, use the same form, but put brackets with a size after the type: new int[40]; /allocates an array of 40 ints new double[size]; // allocates an array of size double // doubles These statements above are not very useful by themselves, because allocation space have no names.

9 Saturday, September 24, new operator contd.. int * p; // declare a pointer p p = new int; // dynamically allocate an int and load address into p double * d; // declare a pointer d d = new double; // dynamically allocate a double and load address into d // we can also do these in single line statements int x = 40; int * list = new int[x]; float * numbers = new float[x+10];

10 Saturday, September 24, delete operator To de-allocate memory that was created with new, we use the unary operator delete. The one operand should be a pointer that stores the address of the space to be deallocated: int * ptr = new int; // dynamically created int //... delete ptr; // deletes the space that ptr points to Note that the pointer ptr still exists in this example. That's a named variable subject to scope and extent determined at compile time. It can be reused: To deallocate a dynamic array, use this form: int * list = new int[40]; // dynamic array delete [] list; list = 0; // deallocates the array // reset list to null pointer After deallocating space, it's always a good idea to reset the pointer to null unless you are pointing it at another valid target right away.

11 Saturday, September 24, Lets see an example!

12 Object Oriented Programming

13 C++ Classes 1. Classes & Objects 2. Data 3. Functions

14 Classes & Objects 1. Blueprint to generate instances of same nature 2. Each individual instance is an object

15 Access Modifiers

16 How to create Objects?

17 Default methods with every class

18 Constructor and Default Methods 1. Constructor(Java and C++) 2. Copy Constructor(C++) 3. Copy Assignment Operator(C++) 4. Destructor(C++)

19 User defined constructors

20 When are objects created on the stack and when are they created on the heap?

21 Lets look at examples

22 Shallow & Deep copy

23 Initialization List

24 Const Data Members

25 Reference Data members

26 Static Data Members

27 Constant Functions

28 Operator Overloading

29 Operator Overloading class pair { public: int x,y; bool operator < ( const pair& p ) const { if(x==p.x) return y<p.y; return x<p.x; } };

30 Static functions

31 Self Referential Classes

32 Friend Classes & Functions

33 Thank You! Prateek Narang prateek@codingblocks.com