For this recipe, we require three files. The first is the implementation that we wish to test (we can call the file leaky_implementation.cpp):
#include "leaky_implementation.hpp"
int do_some_work() {
// we allocate an array
double *my_array = new double[1000];
// do some work
// ...
// we forget to deallocate it
// delete[] my_array;
return 0;
}
We also need the corresponding header file (leaky_implementation.hpp):
#pragma once
int do_some_work();
And, we need the test file (test.cpp):
#include "leaky_implementation.hpp"
int main() {
int return_code = do_some_work();
return return_code;
}
We expect the test to pass, since the return_code is hardcoded to 0. However, we also hope to detect a memory leak, since we forgot to de-allocate my_array.