CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE 4. 141017.

Slides:



Advertisements
Similar presentations
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Advertisements

Computer Programming for Engineering Applications ECE 175 Intro to Programming.
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
Pointers OVERVIEW.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
1 Writing a Good Program 8. Elementary Data Structure.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE C++ INTERLUDE 1.3. C++ BOOK.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Cs 141 Exam 2 Review1 Game Show!. Cs 141 Exam 2 Review2 Whats wrong with this function bool foo (int, double){ return true; }
Data Types Storage Size Domain of all possible values Operations 1.
11 Introduction to Object Oriented Programming (Continued) Cats.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
CSS342: Introduction1 Professor: Munehiro Fukuda.
5.13 Recursion Recursive functions Functions that call themselves
Introduction to Programming
Lecture 9 Files, Pointers
Doubly Linked List Review - We are writing this code
Linked lists.
Dynamic Memory CSCE 121 J. Michael Moore.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
Pointers and Dynamic Variables
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 16-2 Linked Structures
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Popping Items Off a Stack Lesson xx
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Dynamic Memory A whole heap of fun….
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Programming Abstractions
Review & Lab assignments
File Handling in Java January 19
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
CS148 Introduction to Programming II
Linked lists.
Presentation transcript:

CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE

Today’s Agenda Collect topics / questions Homework questions. Present / code snippets FileIO Memory Leaks Dangling Pointers Stack with a linked list Turn this into a sorted list In-class assignment: the list

Homework questions.

File IO In C:In C++:In Java: FILE *fp;ifstream inFile(“name.dat”);import java.io.*; orFileInputStream infile = ifstream inFile;new FileInputStream( “name.dat” ); if ((fp = fopen( “name.dat”, “r” ))inFile.open(“name.dat”);ObjectInputStream input = != NULL {if ( inFile ) { // true of falsenew ObjectInputStream( infile ); fscanf( fp, “%d”, &i );inFile >> i;try { i = input.readInt( ); fclose(fp);inFile.close( );} catch ( EOFException e ) { }}input.close( ); } Note: for output, use ofstream.

File IO example Read in from the console the name of a file. The file contains strings (names) which is your team’s roster. Read in your roster; close the file. User inputs to console a name to look for on the team. Confirm or deny.

cout << "Input file" << endl; cin >> fName; inFile.open(fName); while (getline(inFile, name)) { roster.push_back(name); } inFile.close(); cout << "Input player you are looking for: "; cin >> name; bool onTeam = false; for (int i = 0; i < roster.size(); i++) { if (roster[i] == name) { onTeam = true; break; } if (onTeam) { cout << name << " is on the team." << endl; } else { cout << name << " is not on the team." << endl; }

Review Dynamic Memory Bird *pPenguin; pPenguin = new Bird(“Penguin”); delete pPenguin; pPenguin = (Bird *) malloc(10 * size(Bird)); free pPenquin;

Dynamic memory exercise 1 Create a program or function which leaks memory Run it until memory is exhausted

Bird* CreateFlock(int size, string name) { int i = 0; Bird *flock; flock = new Bird[size]; for (int i = 0; i < size; i++) { flock[i].setName(name); flock[i].setID(i); } return flock; } birdFlock = CreateFlock(size, bName); But do not delete birdFlock

Dangling References: common causes A pointer which is initialized but not set to NULL Delete or free is called and pointer is not set to NULL Aliasing of pointers which are not updated in tandem

Dynamic Memory Exercise 2 Create a dangling pointer De-reference it

Let’s build an Int Stack Use a linked list as a data structure Use the following “node” structure struct Node { int value; Node *next; };

Let’s build an Int Stack Use a linked list as a data structure Use the following “node” structure struct Node { int value; Node *next; } ; Overload the following operators: << Assign = +

Computer Scientist of the week Dennis Ritchie Created C Programming Language Co-creator of Unix “I think the Linux phenomenon is quite delightful, because it draws so strongly on the basis that Unix provided.”

Let’s build an Int Stack Use a linked list as a data structure Use the following “node” structure struct { int x; Node *next; } Node; Overload the following operators: << Assign = + Change into a sorted list

Let’s build an Int Stack Use a linked list as a data structure Use the following “node” structure struct { int x; Node *next; } Node; Overload the following operators: << Assign = + Change into a sorted list Add Remove / Peek function