Introduction to CSCE 221 - Lab

Introduction to CSCE 221 - Lab

C++ REVIEW POINTERS AND TEST DRIVEN DEVELOPMENT TOPIC FOR TODAY C++ POINTERS POINTERS & MEMORY 0x00 0x04 0x08 0x0B 0x10 0x14 0x1 8

0x1 B 0x2 0 POINTERS & MEMORY int x = 5; 0x00 0x04 0x08 0x0B 0x10 0x14 5

x 0x1 8 0x1 B 0x2 0 POINTERS & MEMORY

operator & takes the memory address of an object/ variable int x = 5; int* y = &x; 0x00 0x04 0x08 0x0B 0x10 0x14 5 0x04

x y 0x1 8 0x1 B 0x2

0 POINTERS & MEMORY int x = 5; int* y = &x int* z = y; 0x00 0x04 0x08 0x0B 0x10 0x14 0x1 8 5

0x04 0x04 x y z 0x1

B 0x2 0 POINTERS & MEMORY int x = 5; int* y = &x int* z = y; *z = 0; 0x00 operator * dereferences a

pointer in memory to access the underlying object/data 0x04 0x08 0x0B 0x10 0x14 0x1 8 0 0x04

0x04 x y z 0x1 B 0x2

0 ALLOCATING MEMORY USING NEW Point *p = new Point(5, 5); new allocates space to hold the object. new calls the objects 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 Point *p = new Point(); On the Stack / Automatic allocation Point p;

Point *ps = new Point[n]; Point ps[10]; What happens when p goes out of scope? DEVELOPMENT (ONE OUT OF MANY PERSPECTIVES) 1. Solve 2. Implement 1. 2. 3.

Write test Write code 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 dont 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 dont 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 isnt 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!

Recently Viewed Presentations

  • Steven F. Ashby Center for Applied Scientific Computing Month ...

    Steven F. Ashby Center for Applied Scientific Computing Month ...

    Michael Steinbach Ph.D. Defense Outline Introduction Extending association analysis to non-binary data and non-traditional patterns Generalizing the notion of support Generalizing the notion of confidence Creating new types of association patterns Analyzing the structure of association patterns Conclusions and future...
  • Pronunciation - englishbookeducation

    Pronunciation - englishbookeducation

    Get students to read a line of dialogue, a chant or poem as robots first (which some do without prompting anyway! Good to exaggerate though!) Then ask them to try different intonation by thinking about the different ways/moods/intentions with which...
  • Recursion Recursion What is recursion? When one function

    Recursion Recursion What is recursion? When one function

    Recursion * Every other move is the smallest disc The smallest disc moves in the same direction There are 2^n-1 moves total * "Divide and conquer", Caesar's other famous quote "I came, I saw, I conquered" Divide-and-conquer idea dates back...
  • Oxy fuel Cutting and burning Inspecting the system

    Oxy fuel Cutting and burning Inspecting the system

    Oxyfuel is a group of processes that use heat generated as a result of the combustion of a mixture of oxygen and a combustible gas (a fuel gas). Propane, Natural, MAPP, Propylene, and Acetylene are most common fuel gasses. Each...
  • Genetics EQ: How are our genes passed to

    Genetics EQ: How are our genes passed to

    Mendel's Use of Pea Plants for Genetics Experiments . Pea flowers are normally _____. Since the male and female reproductive structures are relatively enclosed inside the flower, the sperm of the flower will fertilize the egg of the same flower.
  • Redox Reactions Or How Batteries Work REDOX Reactions

    Redox Reactions Or How Batteries Work REDOX Reactions

    When trying to remember which is which think of LEO the lion goes GER OIL RIG. Lose Electrons . Oxidation Oxidation. is Losing. Gain Electrons . Reduction Reduction. is Gaining. Turn to Page 9. Use a chart to determineOxidation States....
  • Sample Title Slide Presentation Title Here

    Sample Title Slide Presentation Title Here

    Associate a tile coordinate with the wire and a run-time router can keep a list of wires to avoid JRoute has a method accessable to user to mark an individual wire Reconfigurable CAM CAM stands for Content Addressable Memory give...
  • Stellar Cusp - National Radio Astronomy Observatory

    Stellar Cusp - National Radio Astronomy Observatory

    Spectral index for both states similar, ~1.3 SSC-model fits very well to data Upper limit of 15 min on time lag NIR/X-ray Eckart, Morris, Baganoff, Schödel et al., in preparation Radio: Zhao, Falcke, Bower, Aitken, et al. 1999-2003 X-ray: Baganoff...