C++ is a high-level, general purpose object oriented programming language. It was created by Bjarne Stroustrup, a Danish computer scientist, as an extension of C programming language in early 80’s to add support of the Object Oriented Programming (OOP) features. Over time it has been extended to support wide range of programming features and abilities. … Continue reading “C++ Object Oriented Programming”
Brian Kernighan’s Algorithm to Count the Set Bits of a Number
To count all set bits of a number, we can loop through all the bits and check whether any bit is set. For example, an integer has 32 bits. We can check all 32 bits in a loop to count the set (1) bits. Here will see how we can improve this mechanism using Brian … Continue reading “Brian Kernighan’s Algorithm to Count the Set Bits of a Number”
Implement Bubble Sort in C
Bubble Sort is the most popular sorting algorithm that most programmers start with. Even though its performance is not good enough for most practical cases, it is much simpler than other efficient sorting algorithms like quick sort or merge sort. Bubble Sort Algorithm Bubble sort is to continuously swap two adjacent elements if they are … Continue reading “Implement Bubble Sort in C”
std::weak_ptr in C++
The std::weak_ptr is part of the smart pointer family introduced in C++11 which holds a non-owning reference to an object managed by other std::shared_ptrs. It does not own the object, which means that the existence of a std::weak_ptr does not ensure the validity of the referred object. If all the std::shared_ptrs, owing the object, get … Continue reading “std::weak_ptr in C++”
std::async – Execute a Function Asynchronously
The function template, std::async, allows us to run a function (possibly time consuming) asynchronously. The actual function, that needs to be executed asynchronously, is passed to this std::sync. The std::sync returns a future object immediately without waiting for the passed function to finish execution. When the function execution will start depends of the options passed … Continue reading “std::async – Execute a Function Asynchronously”
C++ Promise and Future
C++11 introduced the concept of promise and future to a provide synchronization point between threads by sharing objects. The promise object owner (the producer thread) promises to produce a value – may not be immediately but sometime in future. The future owner (the consumer thread) expects a value once the associated promise is met by … Continue reading “C++ Promise and Future”
std::move vs std::forward
The std::move and std::forward are used to serve two different purposes. The std::move enables move semantic for efficient resource transfer from one object to another. Whereas, the std::forward is used to preserve the value category (lvalue or rvalue) of the parent function’s template parameter in time of calling another function. This feature is also known … Continue reading “std::move vs std::forward”
rvalue reference in C++
The rvalue reference is probably the most significant feature introduced by C++11. Let’s quickly recap what rvalue is. It is an inherited concept from C. The expression that can appear only on the right side of an assignment statement is an rvalue. However an lvalue can appear on either side of an assignment statement. But … Continue reading “rvalue reference in C++”