Category Archives: C++

Example of Test Driven Development with C++ and Google Test

To develop a software based on “Test Driven Development” one should follow the following steps:

so let say we want to develop a binary search tree,  here I’m using Google Test.

Step 1, Write enough failing Test Code

so the first step would be something like this:

Step 2, Write Production code to pass the failing tests.

The code from first step will fail during compilation, so the next step is to add the production code such that we can compile and pass the test:

Now this will give us a test failure:

and by adding more production code:

this will pass the test.

Step 3, Refactor the production code

step 3 is refactoring, by doing refactoring we can clean our code to this:

Now we have to jump step1 again, add more test and continue.

Dijkstra’s algorithm with C++

Dijkstra’s algorithm is an algorithm for finding the shortest path in a graph.
First drawing the graph:

 

ref [1]

Standard Exception Handler in C++ and Custom Exception Handler with Examples

Exception handling

C++ Standard Exceptions

List of Example

 

Finding Memory leaking, Stack and Heap overflow

When you access an array index, C and C++ don’t do bound checking. Segmentation faults only happen when you try to read or write to a page that was not allocated (or try to do something on a page which isn’t permitted, e.g. trying to write to a read-only page), but since pages are usually pretty big (multiples of a few kilobytes), it often leaves you with lots of room to overflow.

If your array is on the stack, it can be even worse as the stack is usually pretty large (up to several megabytes). This is also the cause of security concerns: writing past the bounds of an array on the stack may overwrite the return address of the function
and lead to arbitrary code execution (the famous “buffer overflow” security breaches).

By setting the following flags you can find the issue:

Example:

References: [1]

Kalman Filter Explained With Python Code From Scratch

This snippet shows tracking mouse cursor with Python code from scratch and comparing the result with OpenCV. The CSV file that has been used are being created with below c++ code. A sample could be downloaded from here 1, 2, 3.

Python Kalman Filter

C++ and OpenCV Kalman Filter

Rapidcsv has been downloaded from here

 

Eigen unaryExpr (Function Pointer, Lambda Expression) Example

 

Matrix Decomposition with Eigen: QR, Cholesky Decomposition LU, UL

 

 

Eigen Memory Mapping

 

 

Eigen Arrays, Matrices and Vectors: Definition, Initialization Resizing, Populating and Coefficient Wise Operations

 

 

Decomposing Projection Using OpenCV and C++

 

 

And the output is: