Konda.eu

Category Archives: C++

No comments

Measuring Decibels from Microphone

This year was my third year as a mentor at a summer computer camp. We all had lots of fun, the only problem was that the participants - mostly teenage kids at the beginning of the high school - were quite talkative. Loud. To solve this problem a colleague and I came up with a nice solution for the next year.

Continue reading

No comments

Playing Video from WebCam with OpenCV and C++

Cross platform video grabbing can sometimes be really painful. Another great thing about OpenCV is that it solves such problems for you with a couple of clean lines of code.

#include <iostream>
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

int main()
{
    VideoCapture cam(0);

    if (!cam.isOpened())
        return -1;

    cv::Mat frame;

    while (true)
    {
        cam.grab();
        cam.retrieve(frame);
        imshow("Cam:", frame);
        cv::waitKey(1);
    }

    return 0;
}
No comments

Install, compile and link OpenCV from Terminal on Linux

Almost all my development is done in Visual Studio. Currently the best IDE in my opinion, but such tools can quickly spoil you. After being thrown in front of a Linux with only a terminal and an OpenCV dependent program that needed to compile, it was a struggle, but I found the following line to compile everything:

g++ `pkg-config --cflags --libs opencv` source.cpp -o program

And in case you haven't got OpenCV yet installed, you can use simple apt-get command:

sudo apt-get install libopencv-dev
No comments

C++ - Optimizing away vtable with metaprogramming


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

In every object oriented language class inheritance is a life saver when it comes to program architecture. A rather nasty side effect that comes with it is usually performance degradation. And in C++ that penalty can be quite noticeable. When working with virtual methods in derived classes compiler generates vtable (Virtual Method Table), where address for each overridden method is specified. Vtable is then used every time virtual method is called to lookup that address, jump to it and execute code at that location. That penalty can quickly increase execution time by \(\). There is a neat trick that allows us to resolve those addresses compile-time allowing compiler to optimize vtable away using metaprogramming.

Continue reading

No comments

C++ - Unicode Strings! Say Goodbye to std::string

During my years of programming in C++, the biggest pain were probably strings. There is no simple standard cross platform way for Unicode strings. std::string class by itself is also lacking a lot of methods for every day work. Comparing it to C#, Java or any other language is almost useless. Features as starts withends withto lower, to upper, trim and similar are all missing. It has ==, while Java doesn't. Yes, all of the functions above can be easily written with templates but it adds an extra load of work and maintenance, which we all try to avoid.

After a couple of years I finally decided to try ICU - International Components for Unicode. It's a C++ library for Unicode strings. It weights roughly 20MB when shipped with executable, but it's totally worth it. Linux, Apple, Google Chrome and other mayor companies or products use it. All of the above functions are provided and a couple more, all mayor code tables are supported, file access is with already familiar C like interface and as a bonus it's fully integrated with C++ streams.

icu::UnicodeString str = L"Hi ICU with öäüšćž!";
str = str.toLower();
str = str.append(" Foo!   ").trim();

cout << str << endl;

I urge you, give ICU a try!

No comments

C++ - OpenBLAS Matrix Multiplication


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Matrices are extremely popular in many fields of computer science but many operations are slow, especially the useful ones like matrix multiplication where the complexity reaches \(\). There are of course algorithms to speed things up, but there are much faster ways that can fully utilize computer's hardware.

Every operation when doing matrix multiplication is independent which means it can be parallelized through multiple CPU cores or even put on a GPU if you want the best you can get. But sometimes just CPU is enough to avoid expensive copies between CPU and GPU and to reach speed ups up to 10 times. This is where OpenBLAS comes in.

Continue reading

zoom
No comments

C++ - The value of ESP was not properly saved across a function call

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

Another run time error that is almost impossible to quickly identify and debug. It usually occurs when libraries are involved and use different calling conventions e.g. _stdcall instead of _cdecl or other. But sometimes compiler misses a thing or two and compiles something that it shouldn't, after all, compiler only need to know how many bits and bytes to move and what do to with them to translate your program to an executable or a library. Sometimes it lets you do stupid things...

Continue reading

zoom
No comments

C++ - everything to know about Time and Timers

For years, since 1983 when C++ was first released, developers were stuck with C time library, packed in ctime header file. It contains a couple of functions and precision up to a second sometimes just isn't enough. For everything else clock() should be used or OS (Operating System) provided function, which really breaks code portability. In C++11 things got much better, with Chrono module.

Continue reading

zoom
No comments

C++ Lambda Expressions

C++ is currently going through some extensive updates started with the new C++11 standard back in 2011. One of the exciting and very powerful new features are anonymous functions also knows as lambdas. You can simply write them inline in your source code and pass them around as function pointers. An example of sorting vector of ints in a descending order:

vector<int> vec = { 10, 1, 5, 6, 7, 3, 5 };

sort(vec.begin(), vec.end(), [](int x, int y) { return x < y; });

Continue reading