C++ and Catch – Adding your Own Main Method

When you begin coding on a project, it is perfectly acceptable and even advisable to allow the Catch library to generate the main method for you.  That is what the #define CATCH_CONFIG_MAIN (very first line in the tests.cpp file)  directive tells Catch to do.

As you transition from implementing the data structures to implementing a higher-level project, you will want to eventually create your own main method.  Here is how to transition to using your own main without getting rid of tests and testing.

In QtCreator, follow these steps

  1. Add a new cpp file to your project that will contain your main driver.  If you still have the original main.cpp that was added when you created the project, that is fine to use as well; make sure it is listed in the project explorer on the left side of the code window.
  2. Comment out#define CATCH_CONFIG_MAIN at the top of the tests.cpp file.  This will tell the Catch library NOT to generate its own main method.
  3. In your main driver file, copy and paste the following code (to start with). Read the comments throughout to help you understand what is going on.
//CATCH_CONFIG_RUNNER tells the catch library that this 
//project will now explicitly call for the tests to be run. 
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

//A macro used in main to determine if you want to run
//the tests or not. If you don't want to run your tests,
//change true to false in the line below.
#define TEST true

/*
* runCatchTests will cause Catch to go ahead and
* run your tests (that are contained in the tests.cpp file.
* to do that, it needs access to the command line
* args - argc and argv. It returns an integer that
* ultimately gets passed back up to the operating system.
* See the if statement at the top of main for
* a better overview.
*/
int runCatchTests(int argc, char* const argv[])
{
    //This line of code causes the Catch library to 
    //run the tests in the project. 
    return Catch::Session().run(argc, argv);
}

int main( int argc, char* const argv[] )
{
    //If the TEST macro is defined to be true,
    //runCatchTests will be called and immediately
    //return causing the program to terminate. Change TEST
    //to false in the macro def at the top of this file
    //to skip tests and run the rest of your code.
    if (TEST)
    {
        return runCatchTests(argc, argv);
    }

    //start working on other parts of your project here.
    return 0;
}

Once you’ve added that code, rebuild your project (Build menu| Rebuild All) then execute your project.  Your tests should run as normal.

Let’s Review Pointers

Pointers cause a lot of heartburn among students.  Hopefully this post will address some or all of the things you may be struggling with in the world of pointers.

For pointers to make sense, particularly the parts of them that are important for this class, you need to remember a few fundamental pieces of information:

  • Each location where data can be stored has an address.
    • Analogy: Every post office box in Hughes Trigg has an individual address.  If each didn’t, then the workers wouldn’t know what mail goes in which box.
    • Note that even pointers have addresses.  So, if I have [Read more…]

Memory Diagrams

The fact that c++ allows programmers to manage memory directly is one of its great strengths. It is also something that can lead to a ton of tough debugging and hair-pulling.  There are quite a few “things” a programmer can use to avoid most of these problems, but it is important to understand what’s going on under the hood of C++ so that you can fully understand why those other libraries are useful.

Memory Diagrams can help you not only learn the ins and outs of memory management, but they can also help in a debugging situation as well.  A memory diagram is a drawing that represents the state of the memory used by a program at a particular point in execution.  Of course, it is an abstraction of the actual memory usage, but contains enough detail to be very useful.

A memory diagram usually contains two major sections: 1) stack memory, and 2) heap memory.  These two are usually split between the left and the right on a piece of paper. Here is a template you can have a look at:  Memory Diagram Template.

Here is a link to a screencast on Vimeo I made a couple semesters ago related to drawing memory diagrams.  We’ll also be going over them in class as we talk about memory management.

Some related help:

  • Eric Roberts’ (Stanford) Heap-Stack DiagramsHandout on Heap-Stack Diagrams from their Lab sections (This is a link to some of the PDFs saved in Evernote) – These diagrams are quite a bit more “low level” than the method I use, but the general idea is the same.  The memory diagrams start at Problem 3.  A pdf of the solutions is included as well.
  • Debugging Software Crashes II – quite detailed, but a good resource for understanding memory.

Prep for Data Structures

Data structures is a challenging course.  I routinely receive requests for information about how to prepare for the course or what material to review.  Generally speaking, the most important thing to do is review and get comfortable with C++ and problem solving using C++.  Some of the topics that are of particular importance are:

  • Developing algorithms using fundamental control structures
  • Problem decomposition (breaking a problem down into steps to solve it)
  • Object oriented programming in C++ (classes, inheritance, and polymorphism in c++)
  • Pointers (what they are, how to use them, etc.) including pointers to pointers and arrays of pointers
  • Relationship between pointers and arrays
  • Memory management is a biggie, so I’ll break it down into finer points
    • Dynamic Memory Allocation and deallocation (new, new[], delete, and delete[])
    • What methods you should explicitly include in a class that contains dynamic memory (copy c’tor, overloaded operator =, destructor)
    • Difference between stack and heap (free store)

I don’t expect you to be an expert in all of the topics above, but I would expect that you’ve heard of all of them.  And, at least for some of them, you’ve got a firm understanding of what they are/mean/are used for.

Here are some links to materials from the last time I taught CSE 1342 that you might find useful:

If you’d like any more info on a particular topic, feel free to drop me an email.

Command Line Args in C++

As you’re already familiar with, when you call some functions, you need to pass arguments to them.  So, what about main?  There are two different function headers for he main method in C++ that we can use:

int main (); //header 1

and

int main(int argc, char* argv[]); //header 2

Now, you should ask yourself, “What’s the difference?”  Remember that when you execute a program (either from the command line or by double-clicking on an icon or something similar), you’re really asking the OS to load the executable and begin execution.  When the OS is starting your program, you can use command line arguments to send arguments into the main method.  Consider this program execution:

./myFunGame input.txt output.txt

[Read more…]

Data Structures Intro

The first few weeks of the semester, we’ll take a deep(er) dive into pointers and dynamic memory management using C++.  To get started, I wanted to provide you some links to useful information.

Regarding our first topic, pointers and memory management, here are some links to some other blog posts I did over the summer that might be helpful: