The Memory Manager Project

Size: px
Start display at page:

Download "The Memory Manager Project"

Transcription

1 The Memory Manager Project

2 Objectives The goal of your next project is to simulate the C heap manager A runtime module used to allocate and de-allocate dynamic memory. The "heap" is a large "pool" of memory set aside by the runtime system The two main functions are malloc, used to satisfy a request for a specific number of consecutive blocks; free, used to make allocated blocks available f 2

3 Description Our simulation uses a large block of unsigned chars as our memory pool; and a doubly-linked list to keep track of allocated and available blocks of unsigned char. We will refer to the nodes of this list as blocknodes The info field of each node is of type blockdata An object of type blockdata has attributes blocksize number of bytes in the block free a Boolean flag indicating the status of a block blockptr a pointer to the first byte of the block 3

4 malloc The malloc algorithm has an int parameter request request is the size of the block to be allocated request scans the list until it finds the first blocknode B such that B.free == true B.size request If no such block is found, malloc returns NULL (0) 4

5 malloc If B.size is larger than request, the block is broken up into two blocks The first block's size: request The second's size: B.size-request This requires that we insert a new blocknode C after B to reference the second block (which is free) Then, whether we split the block or not, we set B.free to false set B.size to request return the address B.bptr 5

6 free To implement free(unsigned char *p) we must find the blocknode whose bptr field equals p This is done by traversing the blocknode list If this fails, we terminate the program Otherwise we change the blocknode's free field to true But we don't stop there 6

7 Merging Consecutive free Blocks It should be clear that we want to maximize the size of the free blocks This means there should never be consecutive free blocks Whenever consecutive free blocks occur, they should be merged When we free a block, we need to check the previous and next blocks to see if they are free If so, we must merge the blocks into one big block This may involve the deletion of one or two blocknodes from our list 7

8 Doubly-Linked List Utilities To manage doubly-linked lists, we will use a collection of templated functions We will not need the apparatus of a class here, a struct suffices The definition of dlnode and associated functions will be supplied in the file dllistutils.h We will take the approach used in the text for doubly-linked lists Namely, we will use dummy header and trailer nodes This simplifies the code for many list operations 8

9 Project Files The files used in this project are dllistutils.h blockdata.h blockdata.cpp Do not modify, do not submit MemoryManager.h MemoryManager.cpp Complete and submit testmemmgr.cpp Modify and use for testing; Do not submit 9

10 Source Code

11 dlutils.h #include <iostream> #include <cassert> template <class T> struct dlnode { }; T info; dlnode<t> *prev; dlnode<t> *next; dlnode<t>(t val, dlnode<t> *p, dlnode<t> *n) :info(val),prev(p),next(n){}; 11

12 dlutils.h template <class T> void insertafter(dlnode<t> *trailer, dlnode<t> *current, T newval) { } assert(current!= trailer); current->next = prev next new dlnode<t>(newval,current,current->next); current = current->next; current->next->prev = current; 12

13 template <class T> void printdllist(dlnode<t>* header, dlnode<t> *trailer, const char *sep) { } dlutils.h assert(header!= NULL && trailer!= NULL); dlnode<t> *cursor = header->next; while(cursor->next!= trailer) { std::cout << cursor->info << sep; cursor = cursor->next; } if (cursor->next = trailer) std::cout << cursor->info << std::endl; 13

14 dlutils.h template <class T> void deletenode(dlnode<t>* header, dlnode<t>* trailer, dlnode<t>* current) { } assert(current!= header && current!= trailer); dlnode<t> *hold = current; current->prev->next = current->next; current->next->prev = current->prev; delete hold; 14

15 dlutils.h template <class T> void deletenext(dlnode<t>* header, dlnode<t>* trailer, dlnode<t> *current) { } assert(current!= trailer && current->next!= trailer); deletenode(header,trailer, current->next); 15

16 dlutils.h template <class T> void deleteprevious(dlnode<t> * header, dlnode<t> * trailer, dlnode<t> *current) { } assert(current!= header && current->prev!= header); deletenode(header, trailer,current->prev); 16

17 dlutils.h template <class T> void clearlist(dlnode<t> *p) { } dlnode<t> *hold = p; while(p!= NULL) { p = p->next; delete hold; hold = p; } 17

18 The blockdata Definition // blockdata.h #include <iostream> class blockdata { friend ostream& operator<<(ostream& const blockdata &); public: blockdata(unsigned int s, bool f, unsigned char *p); int blocksize; bool free; unsigned char *blockptr; }; 18

19 The blockdata Implementation // blockdata.cpp #include "dlutils.h" #include "blockdata.h" #include <iostream> using namespace std; blockdata::blockdata(unsigned int s, bool f, unsigned char *p) { blocksize = s; free = f; blockptr = p; } 19

20 The blockdata Implementation // blockdata.cpp ostream &operator << (ostream &out, const blockdata &B) { out << "[" << B.blocksize << ","; if (B.free) out << "free"; else out << "allocated"; out << "]"; return out; } 20

21 The MemoryManager Definition class MemoryManager { public: MemoryManager(unsigned int memsize); ~MemoryManager(); unsigned char * malloc(unsigned int request); void free(unsigned char * ptr2block); void showblocklist(); 21

22 The MemoryManager Definition private: unsigned int memsize; unsigned char *baseptr; dlnode<blockdata>* header; dlnode<blockdata>* trailer; }; void mergeforward(dlnode<blockdata> *p); void mergebackward(dlnode<blockdata> *p); void splitblock(dlnode<blockdata> *p, unsigned int chunksize); 22

23 The MemoryManager Implementation MemoryManager::MemoryManager(unsigned int memtotal) : memsize(memtotal) { } baseptr = new unsigned char[memsize]; blockdata dummyblock(0,false,0); blockdata originalblock(memsize,true,baseptr); header = new dlnode<blockdata>(dummyblock,nullptr,nullptr); trailer = new dlnode<blockdata>(dummyblock,nullptr,nullptr); header->next = new dlnode<blockdata>(originalblock,header,trailer); trailer->prev = header->next; 23

24 The MemoryManager Implementation (partial) void MemoryManager::showBlockList() { printdllist(firstblock,"->"); } 24

25 The MemoryManager Implementation (partial) void MemoryManager::mergeForward(dlNode<blockdata> *p) { // Put your code here } void MemoryManager::mergeBackward(dlNode<blockdata> *p) { // Put your code here } void MemoryManager::free(unsigned char *ptr2block) { // Put your code here } 25

26 The MemoryManager Implementation (partial) void MemoryManager::splitBlock(dlNode<blockdata> *p, unsigned int chunksize) { // Put your code here } unsigned char * MemoryManager::malloc(unsigned int request) { // Put your code here } 26

27 Visual Trace of Operations

28 The MemoryManager Constructor MemoryManager M(2048); h e a d e r t ra ile r p r e v b s iz e b p tr n e x t p r e v b s iz e b p tr n e x t p r e v b s iz e b p tr n e x t fr e e fre e f a ls e tr u e f a ls e fr e e h e a d e r t ra ile r b a s e p t r m e m s iz e

29 q splitblock Example pre v bsize bp tr ne xt 35 true Me m ory Po ol splitblock(q,20); q prev bs iz e bptr next pre v bsize bp tr ne xt true true Me m ory Po ol

30 h e a d e r t ra ile r p r e v b s iz e b p tr n e x t p r e v b s iz e b p tr n e x t p r e v b s iz e b p tr n e x t fr e e fre e f a ls e tr u e f a ls e fr e e h e a d e r t ra ile r b a s e p t r m e m s iz e unsigned char *p1 = M.malloc(10); h e a d e r t r a il e r p re v 0 b s iz e 0 fr e e fa ls e b p tr 0 n e x t p r e v b s iz e 1 0 fr e e t ru e b p tr n ex t p r e v b s iz e fr e e f a l s e b p tr n e x t p re v b s iz e 0 fre e fa l s e b p tr 0 n e x t 0 h e a d e r tr a il e r b a s e p t r m e m s i z e

31 to header prev bsize bptr next 10 0 false prev bsize bptr next true to trailer firstblock memsize 2048 baseptr M Memory Pool unsigned char *p2 = M.malloc(20); prev bsize bptr next prev bsize bptr next prev bsize bptr next 0 10 false false true 0 firstblock memsize 2048 baseptr M Memory Pool

32 p1 = M.malloc(15); pre v bsize bp tr ne xt prev bs iz e bptr next pre v bsize bp tr ne xt prev bs iz e bptr next true 20 fa ls e 1 5 false 2003 true 0 firs tbloc k m em size 2048 b asep tr M Me m ory Po ol

33 Block allocated to p1 When free is called on p1, we must merge the resulting consecutive free blocks to one pre v bsize bp tr ne xt prev bs iz e bptr next pre v bsize bp tr ne xt prev bs iz e bptr next true 20 fa ls e 1 5 false 2003 true 0 firs tbloc k m em size 2048 b asep tr M M.free(p1); Me m ory Po ol prev bsize bptr next prev bsize bptr next prev bsize bptr next 0 10 true 20 false true firstblock memsize 2048 baseptr M Memory Pool

34 Testing Code

35 #include <iostream> #include <cassert> #include "MemoryManager.h" const char * startlist = "\n blocklist start \n" const char * endlist = "\n blocklist end \n" int main() { MemoryManager heaper(2048); cout << "heap initialized\n"; cout << startlist; cout << heaper << endl; cout << endlist; 35

36 cout << "Doing first malloc:\n"; unsigned char * p1 = heaper.malloc(10); cout << "malloc done\n"; cout << startlist; cout << heaper << endl; cout << endlist; cout << "On to the second malloc\n"; unsigned char *p2 = heaper.malloc(20); cout << "malloc done\n"; cout << startlist; cout << heaper << endl; cout << endlist; 36

37 cout << "Next free the first pointer\n"; heaper.free(p1); cout << startlist; cout << heaper << endl; cout << endlist; cout << "Now do a malloc for a block too big for " << "the initial open block\n"; p1 = heaper.malloc(15); cout << "malloc done\n"; cout << startlist; cout << heaper << endl; n\n"; cout << endlist; 37

38 cout << "Next free the most recently " << "allocated pointer\n"; heaper.free(p1); cout << startlist; cout << heaper << endl; cout << endlist; cout << "Next free the middle pointer\n"; heaper.free(p2); cout << startlist; cout << heaper << endl; cout << endlist; } return 0; 38

39 Test Output

40 heap initialized BlockList start [2048,free] BlockList end Executing p1 = malloc(10): malloc done BlockList start [10,allocated] -> [2038,free] BlockList end Executing p2 = malloc(20): malloc done BlockList start [10,allocated] -> [20,allocated] -> [2018,free] BlockList end

41 Executing free(p1): BlockList start [10,free] -> [20,allocated] -> [2018,free] BlockList end malloc for a block too big for the initial open block Executing p1 = malloc(15) malloc done BlockList start [10,free] -> [20,allocated] -> [15,allocated] -> [2003,free] BlockList end

42 Next free the most recently allocated pointer (p1) BlockList start [10,free] -> [20,allocated] -> [2018,free] BlockList end Next free p BlockList start [2048,free] BlockList end

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Name: ID#: Section #: Day & Time: Instructor: Answer all questions as indicated. Closed book/closed

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists Prof. amr Goneid, AUC 1 Linked Lists Prof. amr Goneid, AUC 2 Linked Lists The Linked List Structure Some Linked List

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

CA341 - Comparative Programming Languages

CA341 - Comparative Programming Languages CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/45/lab45.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 4 Integer data is usually represented in a single word on a computer.

More information

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; }; struct Buffer { Buffer(int s) { buf = new char[s]; ~Buffer() { delete [] buf; char *buf; ; struct FBuffer : public Buffer { FBuffer(int s) : Buffer(s) { f = fopen("file", "w"); ~FBuffer() { fclose(f);

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to

More information

Review Questions for Final Exam KEY

Review Questions for Final Exam KEY CS 102 / ECE 206 Spring 11 Review Questions for Final Exam KEY The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR,

More information

Object oriented programming

Object oriented programming Exercises 12 Version 1.0, 9 May, 2017 Table of Contents 1. Virtual destructor and example problems...................................... 1 1.1. Virtual destructor.......................................................

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

Review Questions for Final Exam

Review Questions for Final Exam CS 102 / ECE 206 Spring 11 Review Questions for Final Exam The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR, chs.

More information

Assignment 4: SkipList

Assignment 4: SkipList Assignment 4: SkipList Goals : Working with dynamic arrays, pointers, doubly linked lists For this assignment, you will write a Skip List data structure to store integers. When searching a Skip List, items

More information

การทดลองท 8_2 Editor Buffer Array Implementation

การทดลองท 8_2 Editor Buffer Array Implementation การทดลองท 8_2 Editor Buffer Array Implementation * File: buffer.h * -------------- * This file defines the interface for the EditorBuffer class. #ifndef _buffer_h #define _buffer_h * Class: EditorBuffer

More information

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading What you will learn from Lab 7 In this laboratory, you will learn how to use operator overloading and function

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Object oriented programming

Object oriented programming Exercises 7 Version 1.0, 11 April, 2017 Table of Contents 1. Inheritance.................................................................. 1 1.1. Tennis Player...........................................................

More information

Set Implementation Version 1

Set Implementation Version 1 Introduction to System Programming 234122 Set Implementation Version 1 Masha Nikolski, CS Department, Technion 1 // Version 1.0 2 // Header file for set class. 3 // In this implementation set is a container

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

Data Structures (CS301) LAB

Data Structures (CS301) LAB Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013 Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 14: Object Oriented Programming in C++ (fpokorny@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Wrap Up Introduction to Object Oriented Paradigm Classes More on Classes

More information

CpSc212 Goddard Notes Chapter 10. Linked Lists

CpSc212 Goddard Notes Chapter 10. Linked Lists CpSc212 Goddard Notes Chapter 10 Linked Lists 10.1 Links and Pointers The linked list is not an ADT in its own right; rather it is a way of implementing many data structures. It is designed to replace

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 14: Object Oriented Programming in C++ (ramviyas@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Classes (cont d) More on Classes and Members Group presentations Last time

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/11/lab11.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 1 The purpose of this assignment is to become more familiar with

More information

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

cout << How many numbers would you like to type? ; cin >> memsize; p = new int[memsize]; 1 C++ Dynamic Allocation Memory needs were determined before program execution by defining the variables needed. Sometime memory needs of a program can only be determined during runtime, or the memory

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Multiple Inheritance July 26, 2004 22.9 Multiple Inheritance 2 Multiple inheritance Derived class has several base classes Powerful,

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017 PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017 Objectives Recap C Differences between C and C++ IO Variable Declaration Standard Library Introduction of C++ Feature : Class Programming in C++ 2 Recap C Built

More information

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Looping and Counting. Lecture 3 Hartmut Kaiser  hkaiser/fall_2012/csc1254.html Looping and Counting Lecture 3 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2012/csc1254.html Abstract First we ll discuss types and type safety. Then we will modify the program

More information

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Looping and Counting. Lecture 3. Hartmut Kaiser  hkaiser/fall_2011/csc1254.html Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2011/csc1254.html 2 Abstract First we ll discuss types and type safety. Then we will modify the program we developed last time (Framing

More information

Common Misunderstandings from Exam 1 Material

Common Misunderstandings from Exam 1 Material Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself

More information

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files. C++ PROGRAMMING LANGUAGE: NAMESPACE AND MANGEMENT OF INPUT/OUTPUT WITH C++. CAAM 519, CHAPTER 15 This chapter introduces the notion of namespace. We also describe how to manage input and output with C++

More information

a data type is Types

a data type is Types Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental

More information

EECE.3220: Data Structures Spring 2017

EECE.3220: Data Structures Spring 2017 EECE.3220: Data Structures Spring 2017 Lecture 14: Key Questions February 24, 2017 1. Describe the characteristics of an ADT to store a list. 2. What data members would be necessary for a static array-based

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3 Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 3 2 Coding for Linked Lists 7 2.1 Traversing a Linked List........................... 10 2.2 Searching a Linked List............................

More information

CSE 333 Final Exam June 6, 2017 Sample Solution

CSE 333 Final Exam June 6, 2017 Sample Solution Question 1. (24 points) Some C and POSIX I/O programming. Given an int file descriptor returned by open(), write a C function ReadFile that reads the entire file designated by that file descriptor and

More information

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Dynamic Memory Management Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Dynamic Memory Allocation Dynamic memory allocation is used to

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012 Instructor: K. S. Booth Time: 150 minutes (two hours thirty minutes)

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi. Laboratory 7 Programming Workshop 2 (CSCI 1061U) Faisal Qureshi http://faculty.uoit.ca/qureshi C++ Inheritance Due back on Saturday, March 25 before 11:59 pm. Goal You are asked to create a commandline-based

More information

Variables. Data Types.

Variables. Data Types. Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting

More information

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 8: Object-Oriented Programming (OOP) 1 Introduction to C++ 2 Overview Additional features compared to C: Object-oriented programming (OOP) Generic programming (template) Many other small changes

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Spring 2008 Data Structures (CS301) LAB

Spring 2008 Data Structures (CS301) LAB Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling

More information

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil Linked Lists Gaddis Ch. 17 CS 2308 :: Spring 2016 Molly O'Neil List ADT A list is an abstract data type representing an ordered sequence of values For example, both MP3 Player assignments have used lists:

More information

Introduction & Review

Introduction & Review CPSC 250 Data Structures Introduction & Review Dr. Yingwu Zhu What to learn? ADT design & implementation using C++ class Algorithm efficiency analysis Big-O ADTs: Binary search trees, AVL trees, Heaps,

More information

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

MM1_ doc Page E-1 of 12 Rüdiger Siol :21 Contents E Structures, s and Dynamic Memory Allocation... E-2 E.1 C s Dynamic Memory Allocation Functions... E-2 E.1.1 A conceptual view of memory usage... E-2 E.1.2 malloc() and free()... E-2 E.1.3 Create

More information

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18 CS31 Discussion Jie(Jay) Wang Week8 Nov.18 Outline Pointer Struct Memory Management When the program gets executed, it gets some amount of memory allocated for use. memory Program 1 Program 2 Memory Management

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

CMSC 202 Midterm Exam 1 Fall 2015

CMSC 202 Midterm Exam 1 Fall 2015 1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below.

More information

Topics. bool and string types input/output library functions comments memory allocation templates classes

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

More information

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4 Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 4 1 2 Coding for Linked Lists 8 2.1 Traversing a Linked List............................... 12 2.2 Searching a Linked List................................

More information

Unit 7. 'while' Loops

Unit 7. 'while' Loops 1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted. Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted. For the next five questions, consider the function to

More information

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010. 1 2 3 MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010 1 2 3 efficient updates with lists At http://www.sgi.com/tech/stl/ is the Standard Template Library Programmer

More information

Comp151. Generic Programming: Container Classes

Comp151. Generic Programming: Container Classes Comp151 Generic Programming: Container Classes Container Classes Container classes are a typical use for class templates, since we need container classes for objects of many different types, and the types

More information

Exceptions, Case Study-Exception handling in C++.

Exceptions, Case Study-Exception handling in C++. PART III: Structuring of Computations- Structuring the computation, Expressions and statements, Conditional execution and iteration, Routines, Style issues: side effects and aliasing, Exceptions, Case

More information

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; } Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3;

More information

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

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1 CSE 143 Linked Lists [Chapter 4; Chapter 6, pp. 265-271] Linked Lists A linked list is a collection of dynamically allocated nodes Each node contains at least one member (field) that points to another

More information

Increment and the While. Class 15

Increment and the While. Class 15 Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment

More information

Recitation #11 Malloc Lab. November 7th, 2017

Recitation #11 Malloc Lab. November 7th, 2017 18-600 Recitation #11 Malloc Lab November 7th, 2017 1 2 Important Notes about Malloc Lab Malloc lab has been updated from previous years Supports a full 64 bit address space rather than 32 bit Encourages

More information

Classes in C++98 and C++11

Classes in C++98 and C++11 Classes in C++98 and C++11 January 10, 2018 Brian A. Malloy Slide 1 of 38 1. When we refer to C++98, we are referring to C++98 and C++03, since they differ only slightly. C++98 contained 3 types of constructors,

More information

Lambda functions. Zoltán Porkoláb: C++11/14 1

Lambda functions. Zoltán Porkoláb: C++11/14 1 Lambda functions Terminology How it is compiled Capture by value and reference Mutable lambdas Use of this Init capture and generalized lambdas in C++14 Constexpr lambda and capture *this and C++17 Zoltán

More information

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std; r6.14 For the operations on partially filled arrays below, provide the header of a func tion. d. Remove all elements that are less than a given value. Sol a. void remove_items_less_than(int arr[], int

More information

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit. C++ Basics January 19, 2012 Brian A. Malloy Slide 1 of 24 1. Many find Deitel quintessentially readable; most find Stroustrup inscrutable and overbearing: Slide 2 of 24 1.1. Meyers Texts Two excellent

More information

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works Type Aliases A name may be defined as a synonym for an existing type name. Traditionally, typedef is used for this purpose. In the new standard, an alias declaration can also be used C++11.Thetwoformsareequivalent.

More information

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can

More information

Pointers. Developed By Ms. K.M.Sanghavi

Pointers. Developed By Ms. K.M.Sanghavi Pointers Developed By Ms. K.M.Sanghavi Memory Management : Dynamic Pointers Linked List Example Smart Pointers Auto Pointer Unique Pointer Shared Pointer Weak Pointer Memory Management In order to create

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................

More information

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects MCS 360 Lecture

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/04/lab04.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 3 In this assignment create an IntegerSet class that will provide

More information

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened

More information

Midterm Exam. Sample Solutions

Midterm Exam. Sample Solutions Name: CS 410 Introduction to Software Engineering Fall 2016 Instructor: Marc Pomplun Midterm Exam Sample Solutions No books, no notes, and no calculators are allowed. Question 1: out of points Question

More information

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSE030 Fall 2012 Final Exam Friday, December 14, PM CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need

More information

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM CS 103: Introduction to Programming Fall 2017 - Written Final Exam 12/11/16, 4:30PM 6:30PM Name: USC Email Address: Lecture (Circle One): Redekopp 2:00 TTh Goodney: 2 MW 9:30 TTh 11:00 TTh Complete the

More information

CS 247: Software Engineering Principles. ADT Design

CS 247: Software Engineering Principles. ADT Design CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data

More information

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 2 Yusuf Pisan Compiled helloworld yet? Overview C++ fundamentals Assignment-1 CSS Linux Cluster - submitting assignment Call by Value,

More information

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

More information

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University [CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University CHAPTER 9 Pointers #1~2 Pointer int main () { int a; int b; int c;

More information

School of Information Technology and Computer Science CSCI192 - Spring Session Revision

School of Information Technology and Computer Science CSCI192 - Spring Session Revision School of Information Technology and Computer Science CSCI192 - Spring Session Revision Aim: Revision 1. Covert 0x27 from hexadecimal into 8-bit binary value 00100111 2. Convert 00110110 8-bit binary value

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

W3101: Programming Languages C++ Ramana Isukapalli

W3101: Programming Languages C++ Ramana Isukapalli Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical

More information