Array out of Bounds Normally bounds are unchecked for execution efficiency.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
1 CSC241: Object Oriented Programming Lecture No 28.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
Natalia Yastrebova What is Coverity? Each developer should answer to some very simple, yet difficult to answer questions: How do I find new.
C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Pointers and Dynamic Arrays
5.13 Recursion Recursive functions Functions that call themselves
Doubly Linked List Review - We are writing this code
CISC181 Introduction to Computer Science Dr
This pointer, Dynamic memory allocation, Constructors and Destructor
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 16-2 Linked Structures
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Pointers & Functions.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
9-10 Classes: A Deeper Look.
Passing Arguments and The Big 5
Dynamic Memory.
Chapter 9: Pointers and String
Pointers & Functions.
Passing Arguments and The Big 5
The Stack.
Pointers, Dynamic Data, and Reference Types
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
Presentation transcript:

Array out of Bounds Normally bounds are unchecked for execution efficiency

int all[] = {5,6,7,8,9,10,11,12,13,14}; string names[] = {"dasher", "dancer", "prancer", "vixon", "comet", "cupid", "donner", "bliltzen", "rudolph", "Joseph"}; all[10] = 47; // Error doesn't show up until after it completes running normally // Error:Run-Time Failure Check #2, stack around the variable "all" was corrupted

Shallow and Deep Copy When you pass in an object into a method (rather than a pointer to the object), a copy is created. When you exit the method, the destructor is used to delete the object. The problem is the combination of a shallow copy on entering the method and a deep destroy.

class OListNode { public: OListNode(Object &theElement,string aname, OListNode * n = NULL ) : element( theElement ), next( n ) { name = new string(); *name = aname; cout << "Constructing the node at " << toString() << endl; } ~OListNode(){ cout << "destroying the node at " << toString() << endl; delete name; } string toString() { return *name; } Object & element; string *name; OListNode *next; };

void changeCopy(OListNode o) { *(o.name) = "SuperMan"; cout << "Changed Node " << o.toString() << endl; } int x = 100; OListNode * o = new OListNode (x, "Clark Kent"); cout toString() << endl; changeCopy(*o); cout toString()<< endl;

Original Object (dynamically allocated string)

Mystery Error

void Graph::longLength() {queue nodeList = topsort(); while (! nodeList.empty()) {GraphNode curr = nodeList.front(); nodeList.pop(); for( EdgeNode * e = curr.adj ; e != NULL ; e = e->next) { if( curr.length + 1 > G[e->Tail].length) G[e->tail].length = curr.length + 1; } After running, all nodes with zero predct have length of 0, all others have 1. WHY?