Skip to main content

Posts

GDB Automatic Deadlock Detector

Have you ever had a problem with detection deadlock between threads in your C/C++ application? Would you like to do that automatically? Try this GDB Automatic Deadlock Detector from my github: GDB Automatic Deadlock Detector Picture source: http://csunplugged.org/wp-content/uploads/2015/03/deadlock.jpg1286488735

How to quickly investigate memory leak in your application

Every C/C++ software developer meet problem of memory leak (allocating memory over time which is not freed). If you would like to know how to investigate memory leak problem in your application, this article is for you. For memory leak investigation I recommend to use valgrind tool. Especially with it's Massif tool. Let's see the example how we could use that tool for memory leak investigation. Below snippet of code has lots of points where memory leak can occur. 1 #include <stdio.h> 2 3 void allocmem1 () 4 { 5 char * memleak 2 = malloc ( 100 * sizeof ( char )) ; 6 } 7 8 void allocmem2 () 9 { 10 char * memleak 2 = malloc ( 10 * sizeof ( char )) ; 11 allocmem1 () ; 12 } 13 14 void allocmem3 () 15 { 16 char * memleak 2 = malloc ( 5 * sizeof ( char )) ; 17 allocmem2 () ; 18 } 19 20 int main ( int argc , const char ** argv ) 21 { 22 char * memleak 1 = malloc ( 5 * sizeof ( char )) ; 23 ...

How to speed-up make-based building process

If you are building some multifile-based project using make tool, it is worth to use parallel build in order to speed up building process. You can do that using -j option  of make tool ex. make -j2 If you will select -j2, you are informing your compiler to use 2 cores of your CPU for building process. The optimal option is to split build using all cores of your CPU. In order to get number of cores of your CPU on Linux-based OS you can use command: nproc Therefore, if you would like to speed-up build time optimally using all cores of you CPU, you can use following command on Linux-based OS: make -j`nproc` Picture source: http://hostme.ie/wp-content/uploads/2011/03/speed-up-wordpress.jpg

Linux - VIM as Integrated Development Environment (IDE)

In this post I would like to present you my configuration of vim text editor as Integrated Development Environment (IDE). In my daily work I am not using big IDE environments like Eclipse, NetBeans or other. Instead, I am using vim text editor configured with number of plugins, which contains all of most important features of traditional IDE environment and additionally I don't need to resign with VIM advantages like speed and flexibility. Using that VIM as IDE configuration I comfortably work with following programming languages on the daily basis: C/C++ Java Python Bash JavaScript HTML CSS My VIM as IDE configuration is publically available in github. You can clone it from here: git clone https://github.com/xmementoit/vim-ide It is under continous development so you can expect adding new features in the future. Video presentation of main features of my VIM as IDE environment can be found on youtube: I am aware that starting with big VIM as IDE configurati...

Bash - useful bash aliases which I use in my daily work

~/projects/CppAdventureExamples/bash/aliases.html Bash alias is abbreviation of one terminal command (usually long command) using other terminal command (usually much shorter command). We can define aliases in bash dot files (ex. ~/.profile file). Example of bash alias: alias i="sudo apt-get install" Then we can install 'vim' package using 'i' alias this way (it works of Debian-based systems): i vim Today I would like to present few useful bash aliases which I use in my daily work. You can paste below aliases into your ~/.profile file (or ~/.bashrc) if you recognize them useful for you. Enjoy! #================================================================== # Listing aliases alias ll= "ls -lah" #list only directoriesj alias lsd= "ls -lF ${colorflag} | grep --color=never '^d'" alias sl= 'ls' #================================================================== #filter selected app fro...

Bash - parallel operations

Today I would like to present useful tool for every Linux developer - bash parallel mode. Bash parallel mode allows to invoke multiple bash tasks (ex. functions, linux applications etc.) in parallel, which could save time of processing them. What's is more, usage of bash parallel mode is very simple and does not require lot of work. Let's explain it based on the examples. We are starting with bash script working in seqence mode (one function invoked after previous one). We defind function task() which is later invoked in for loop 10 times. Invoking of each instance of function takes SLEEP_TIME=2 seconds. So script invokation takes 10x2s == 20 seconds. Now, lets change above script in order to work in parallel mode: Please note few small, but important changes between above scripts: < invokation of function task has '&' character at the end. It means that function will be invoked in background and will not block main thread of script. functio...

C++11 Multithreading - How to avoid race conditions

This article presents how to avoid problem of Race conditions described in this article. As we already know race conditions can occur when two or more threads are trying to invoke some part of code or use the same resource (ex. variable) at the time. That can cause problem of unexpected results. We could see that problem in 'Race conditions' article where two threads was able to modify the same variable value which caused different program output every time we invoked it. In order to avoid that problem we should synchronize operations of our threads using multithreading mechanism called mutexes (abbr. Mutual exclusion ). Mutex is some kind of program flow control lock which which is able to make sure that some part of code locked by itself can be invoked by only one thread at the same time. Such locked part of code is called critical section . When other thread will try to go into critical section it will be blocked and will need to wait until previous thread...

Software Adventure - new blog name

I decided to change name of this blog, because I am going to present not only C++ topics here. From today you can expect topics describing problems and features of following domains of software development: C++ programming language features, problems, issues C  programming language features, problems, issues Unix/Linux programming (ex. standard Linux C library, system calls etc.) Embedded Linux programming (problems and solutions related to Embedded Software Development) Software Development Design - Object Oriented Programming, Design Patterns etc. Software Development Life Cycle Sometimes I will also present some articles related to other programming languages like: Python, Java, JavaScript, HTML, CSS etc.  As you noticed range of topics in this blog has been significantly extended. It should this blog more interesting.

C++11 - User-defined literals

C++11 standard provides new way of customized literal constants called user defined literals . Thanks to that user can define own suffixes for standard literals (ex. digits) which defines it better and makes it better readable. Example of such suffixes could be: _meters, _kilograms, _squareMeters etc. For better understanding user-defined literals concept, let's take a look on the below example code: Output of that code is: In point one we can see concept of operator overloading function which should be used in order to define user-defined literals. In our example we are defining suffixex _meters and _squareMeters which will be used for imporve readability of code of calculations based on that units. Point II shows how we can use user-defined literals to assign values to variables. Such usage definitely makes code better readable and easier to understand. Point III, shows how we can use other suffix (_squareMeters) in comparison calculations. As we can see, our units a...

C++11 - noexcept function specifier

Today I would like to present another new feature of C++11 standard - noexcept specifier. This specifier allows to specify function which does not throw any exception . It should be used be used wherever possible to notify user that function should be throw and it makes such function non-throwable self-documented (similar to usage const ). Its usage is similar to usage const function specifier. The difference is that while const function tries to modify data it we have compilation error. In case of noexcept function throws any exception, code compiles, however it std::terminate function is invoked when we achieve throwing exception in such function. To understand it better, take a look on below example: Output of this example is: In point I, we are defining function as non-throwable using noexcept specifier. Such function should not throw, however in our example we are throwing exception in order to present what happens when noexcept function tries to throw except...

Advanced C++ - Stack unwinding

Stack unwinding is normally a concept of removing function entries from call stack (also known as Execution stack, Control stack, Function stack or Run-time stack). Call Stack is a stack data structure that stores active functions' addresses and helps in supporting function call/return mechanism. Every time when a function is called, an entry is made into Call stack which contains the return address of the calling function where the control needs to return after the execution of called function. This entry is called by various names like stack frame , activation frame or activation record. With respect to exception handling , stack Unwinding is a process of linearly searching function call stack to reach exception handler. When an exception occurs, if it is not handled in current function where it is thrown, the function Call Stack is unwound until the control reaches try block and then passes to catch block at the end of try block to handle exception. Also, in this proc...

Advanced C++ - Exceptions

Exception handling is programming feature helpful for programmers to handle run time error conditions. Run time error conditions can be like division by zero, unable to allocate memory due to scarcity, trying to open a file which doesn’t exist etc. There are two types of exceptions. One is Standard exceptions provided by C++ Standard library and other type is User defined exceptions. Now, to handle any exception, we should understand 3 main block of exception statement which are as follows: throw block - in this block, we throw an exception. The operand of the throw statement decides the type of exception occurred. Once this line is executed, program jumps to catch block to handle the exception occurred. catch block - is the block where we handle exception. This is also called as exception handler and this block is written immediately after the try block as shown in example. This can be written similar to a normal function with at leas...

ACCU 2014 Conference - Bristol, UK

Last week I participated in ACCU 2014 Conference in Bristol (UK). That was great and fruitful time for me where I find out lot of information about C++ and Software development in general, as well as I met few interesting people. I would like to share few information from that conference with you. During the conference we listen interesting speeches of many C/C++ experts such as: Howard Hinnant - he presened interesting speech about C++11 Move Semantics , presenting interesting short explanation how std::move actually work as well as detail presentation of generation of Special Member Function (class members which are automatically generated by compiler) for different cases with and without move constructor and move assignment operator. At the end he presented detail implementation of basic move constructor and move assignment operator . Detail explanation of move semantics will be presented on this blog soon. Anthony Williams - author of book C++ Concurrency in Action and d...

C++ Multithreading - Race conditions

In the previous C++ Multithreading article I presented you how to pass parameters between threads. Take a detail look on the output of that example once again: In the first line of that output you can notice that output text from two threads is mixed. You are probably wondering why it happens? It is because we are not protecting resources which are shared by two threads (in this example cout stream is shared in both threads) which causes multithreading's phenomenon called race condition . Because threads switching and accessing to shared resources are managed by operating system we do not know when std::cout stream will be accessed by main thread and when it will be accessed by second thread. Therefore in the previous article I mentioned that output of the example can be little different on your computer than my output example. What's more it is possible that this output will be different for few consecutive invoking of the example on the same machine. It is beca...

C++ Multithreading - Passing parameters to thread function and sleep thread

Today I am going to present you second article about new C++11 Multithreading feature. I am going to present you how we can pass parameters to thread function (by value and by reference) as well as show you how to delay thread processing for selected amount of time . In previous C++ Multithreading article we presented how to create thread function and run second thread in parallel to main thread. Now we are extending that example with usage of another multithreding features. Let take a look on the new example: Output of that example could be (it can be little other output on your machine because we are working with Multithreading which is not protected by phenomenon called race condition which I will explain in one of another articles): In point I and II we are creating new thread which will invoke function print() from point Ia. Difference to the example from previus article is that we are passing two parameters to our function. First parameter firstParam is passed b...

Advanced C++ - virtual functions

Today I am going to explain some basic but very important C++ feature - polymorphism and inheritance which is based on virtual functions in C++. Virtual functions can be declared by preceding the function with virtual keyword. The importance of Virtual functions can be understood, when we design classes using inheritance. Virtual functions are special functions which are called using late binding concept. Late binding or dynamic binding means that the binding happens during run time using vtable (virtual table) to select the correct virtual method to be called as the code runs. Let us look at an example for getting more clarity on how Virtual functions work in reality: Output of that example is: Point 1 shows how virtual function is declared and defined. Point 2 shows the inheritance concept i.e class Derived is inherited from class Base using public access specifier. In point 3, virtual function of class Base is redefined in class Derived which leads to f...

Advanced C++ - Mutable Class Field

Today I would like to present C++ class' feature called mutable class field . Mutable class field is class' field modifier which allows to change its value even if object of the class is declared as const . Take a look at the example: Output of this example is: In point I of that example we are defining object of TestClass . Note that this object is const . As you can see in point Ia this class has three different member fields ( constInt, mutableConstInt, nonConstInt ). Those variables are public for this example, but do not worry about encapsulation here. It is just omitted for simplify this example. As you can see one of this member fields is marked as mutable class file using mutable keyword ( mutableConstInt ). Such variable can be modified even if object of class TestClass is const . It will be explained in next points of this example. In point II we are printing default values of testObject object initialized in initialization list of TestClass' default c...

QT 5.2 - Changes and future prediction - interesting article

I would like to share with you interesting article about QT 5.2 changes, integration with mobile platform and preditcions of future of QT framework. You can find this article here: http://meetingcpp.com/index.php/br/items/a-look-at-qt52.html Enjoy!

C++11 - Uniform initialization

C++11 standard provides new form of objects initialization which makes such initialization uniform for different structs and classes and simplify objects construction and initialization process. Thanks to the new process we can initialize many different classes using the same syntax. Please take a look on the example below: Output of this example is: In point I we are initializing class of two different objects using C++11 uniform initialization mechanism. On object is created based on struct type (pure struct type without any method - look point II). The second one is created based on MyClass class constructor. As you can see, despite that we are creating objects of two totally different types, those objects can be created the same way (uniform way). This is just uniform unitialization featuere provided in C++11 standard. Below we are initializating MyStruct object using external function getMyStruct() . You can take a look to the body of that function (point IV). ...

C++ in 2014 - Predictions

Today I would like to share with you interesting article about prediction of development C++ programming languages (and its well-known frameworks and libraries) in 2014. It is written by Jens Weller and I think it is very interesting for every C++ programmer and user. You can open this article by clicking on the image below: Enjoy!