C++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT.

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Chapter 18 Vectors and Arrays
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Memory Management CS Chakrabarti Variable storage thus far  Never used global variables  All variables allocated inside functions, passed.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Pointers OVERVIEW.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Object-Oriented Programming in C++
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Workin’ with Pointas An exercise in destroying your computer.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Object Oriented Programming COP3330 / CGS5409.  Aggregation / Composition  Dynamic Memory Allocation.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Introduction to Object Oriented Programming Chapter 10.
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.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Test Driven Development (TDD)
Development and Testing Ch4.1. Algorithm Analysis
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Testing Acknowledgement: Original slides by Jory Denny.
Dynamic Memory A whole heap of fun….
COP 3330 Object-oriented Programming in C++
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

C++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT

TOPIC FOR TODAY – C++ POINTERS

POINTERS & MEMORY 0x000x040x080x0B0x100x140x180x1B0x20

POINTERS & MEMORY int x = 5; 5 x 0x000x040x080x0B0x100x140x180x1B0x20

POINTERS & MEMORY int x = 5; int* y = &x; 5 xy 0x04 0x000x040x080x0B0x100x140x18 0x1B0x20 operator ‘&’ takes the memory address of an object/variable

POINTERS & MEMORY int x = 5; int* y = &x int* z = y; 5 xy 0x04 z 0x000x040x080x0B0x100x140x180x1B0x20

POINTERS & MEMORY int x = 5; int* y = &x int* z = y; *z = 0; 0 xy 0x04 z 0x000x040x080x0B0x100x140x180x1B0x20 operator ‘*’ dereferences a pointer in memory to access the underlying object/data

ALLOCATING MEMORY USING NEW Point *p = new Point(5, 5); new allocates space to hold the object. new calls the object’s constructor. new returns a pointer to that object.

DEALLOCATING MEMORY USING DELETE // allocate memory Point *p = new Point(5, 5);... // free the memory delete p; For every call to new, there must be exactly one call to delete.

USING NEW WITH ARRAYS int* nums = new int[10]; // ok Dynamically allocates an array of 10 integers

USING DELETE ON ARRAYS // allocate memory int* nums = new int[10];... // free the memory delete[] nums; Have to use delete[].

SYNTACTIC SUGAR “->” Point *p = new Point(5, 5); // Access a member function: (*p).move(10, 10); // Or more simply: p->move(10, 10); Dereference the object then call one of its functions

STACK VS. HEAP On the Heap / Dynamic allocation On the Stack / Automatic allocation Point *p = new Point(); Point *ps = new Point[n]; Point p; Point ps[10]; What happens when p goes out of scope?

DEVELOPMENT (ONE OUT OF MANY PERSPECTIVES) 1. Solve 2. Implement 1. Write test 2. Write code 3. Repeat 3. Integrate 4. Release

TEST DRIVEN DEVELOPMENT (TDD)

PRACTICAL EXAMPLE Lets practice some TDD on the following example Your project manager at BusyBody Inc says he needs a feature implemented which determines the average amount of time a worker at the company spends at their desk. He says the number of hours each day is already being measured and is stored in an internal array in the code base.

PRACTICAL EXAMPLE How do we solve this? Compute an average of an array!

PRACTICAL EXAMPLE First we write a test in other words, set up the scaffolding of the code instead of a function which you don’t know if it works or not – and continue to struggle finding bugs template double sum(It first, It last) { return inf; //note this clearly does not work and is thus failing } int main() { vector arr{0, 1, 1, 2, 3, 5, 8}; if(sum(arr.begin(), arr.end()) != 20) cout << “Test failed?!?!?! I suck!” << endl; //you don’t really suck, its supposed to fail! }

PRACTICAL EXAMPLE Before we continue, lets review Positives Scaffolding, function interface, and test all implemented We know it is good design Tests to tell if the code is correct, before we struggle with debugging many lines of code Negatives Code isn’t written until later…..but is that really that bad? NO In fact, with TDD you code FASTER and more EFFECTIVELY than without it

PRACTICAL EXAMPLE Now the code – and then run the test! template double sum(It first, It last) { double s = 0; while(first != last) s += *first++; return s; }

THINGS TO REMEMBER Always have code that compiles Test writing is an art that takes practice (and more learning!) Compile and test often!

TESTING FRAMEWORKS Many frameworks exist CppUnit, JUnit, etc. We will be using a much more simple unit testing framework developed by me If you have downloaded the lab zip for today open it and look there Follows SeTT (Setup, Test, Teardown) – unit testing paradigm

PAIR PROGRAMMING Driver – One at the keyboard Navigator – Helps direct driver Teamwork and communication are key here Switch roles frequently!

TDD - EXERCISE Download the zip file for this exercise Write a c++ function to move an array of integers from one place in memory to another place. Essentially is copy, but delete the original array after copying. Pair program!