Smart Pointers Explained With Simple Example

Standard library included with C++ compilers provides some specific resource management pointers classes which are also called as Smart Pointers.

A normal (raw) pointer typically points to an object, but it doesn’t indicate who owns the objects. This leads to the confusion most of the times, that who is supposed to delete the pointer and free the memory.
Also, by looking into the pointer, one can’t guess how the pointer should be deleted.

Smart pointers are designed to overcome these shortcomings of raw pointers.

Types of Smart Pointers

  • unique_ptr -> This pointer class has the exclusive ownership.
  • shared_ptr -> This pointer class has the shared ownership.
  • weak_ptr -> This pointer class is used with shared_ptr and it doesn’t have any ownership. This pointer is typically used to break loops into circular data structures.
(more…)
Smart Pointers Explained With Simple Example Read More

Thread Synchronization – Mutex Explained With Simple Example

A Mutex is basically a lock that is locked before accessing any critical section of code to ensure only one thread at a time can access it. Mutex is unlocked when the thread is done processing. Thus, mutex can also be thought as “Mutually exclusive flag” to ensure all other threads are blocked to a certain section of code when one thread is already accessing it.

(more…)
Thread Synchronization – Mutex Explained With Simple Example Read More

Inter Process Communications – Pipes Explained With Simple Example

Pipes are one of the oldest and simplest form of Inter-Process communications. Typically, a pipe is half-duplex way of transmitting data between two processes which means data can be passed in one direction only. It can be used only between processes having common parent.
Normally pipes are used between parent and child processes.

(more…)
Inter Process Communications – Pipes Explained With Simple Example Read More

Difference Between Process and Threads

A program in execution is called a process. There can be multiple instances of a program running in the system which are identified by a unique id called process id. Operating System ensures that each process has a unique id which is always a non-negative integer number.

A thread is a lightweight process which can only be used inside a process. Like process, thread also has a unique identifier to identify threads which are called thread ids. Unlike process ids, thread ids are only unique to a process and one process threads are unknown to other process.

(more…)
Difference Between Process and Threads Read More

Process Cleanups Via Exit Handlers Registered Using atexit function

Any normal Unix process can terminate in following ways:

  • Graceful exit – Normal termination of process. For eg:
    • Returning from main, calling exit etc.
  • Ungraceful exit – Abnormal termination of process. For eg:
    • Abort signal. Due to some fatal error etc.

_exit, _EXIT and exit functions can be used to terminate the process normally. Usually exit functions do the cleanup but if needed a programmer can also define its own exit handler functions to do some specific cleanup actions needed by the program.

(more…)
Process Cleanups Via Exit Handlers Registered Using atexit function Read More

fork () Function Explained With Simple Example

Fork () is a system call in unix which can be used by a process to create a new process. This is a special function which is called once but it returns twice, one for parent process who called the fork () and second for the child process which is created by fork (). Fork () returns process id of the new process in parent process whereas it returns 0 for child process.

Syntax of fork () function call is as follows:

#include <unistd.h>
pid_t fork(void);   /* Returns: 0 in child, process ID of child in parent, −1 on error */
(more…)
fork () Function Explained With Simple Example Read More

Dynamic Memory Allocation Explained With Simple Example

Dynamic memory allocation is the process of allocating memory at run time by the program based on the need of the program. Dynamic Memory allocation is done from the heap memory available in the system and handling of memory (creation and deletion) is totally handled by the programmer. In case, programmer forgets to cleanup the allocated dynamic memory block, it will lead into memory leak and this memory block will be blocked for further use until program restarts.

Methods available to allocate dynamic memory at run time are

  • malloc () – allocates one memory block of requested size.
  • calloc () – allocates multiple blocks of memory and initialize it with 0.
  • realloc () – increases or decreases the size of previously allocated memory block.
  • free () – frees the memory allocated by above functions.
(more…)
Dynamic Memory Allocation Explained With Simple Example Read More

C++11: extern template Explained With Simple Example

In C++11 (Introduction to C++11), extern template is added to optimize the compile time and object size. Earlier C++ compiler (C++ 03) instantiate a template function or class whenever it encounters a fully defined template function or class. If the template is used in multiple files, then compiler must create multiple instances of template function or class only to later discard all the instance but one. This would result in extra compile time and increased object file size. In C++ 03 there was no way to avoid this.

In C++11, extern template feature is introduced to avoid this situation.
Format of extern template is as follows

extern template void DemoTemplateFunction <int> ();
extern template class NUM <type>;
(more…)
C++11: extern template Explained With Simple Example Read More

Early vs Late Binding using Virtual Table and VPtr

Early BindingLate Binding
Happens at compile time.Happens at run time.
Compiler has all information to invoke correct function version at compile time.Compiler doesn’t have information to identify correct function version.
Normal function calls are example of Early Binding.Virtual functions are example of Late Binding.
Functions invoked by object is example of early bindingFunctions invoked by pointers can be example of late binding.
Early binding is efficient as no cost paid at run-time for function resolution.Late Binding is slightly costly as function resolution happens at run time.
Early binding doesn’t provide flexibility of one method multiple interfaces.Late Binding provide flexibility.
Doesn’t have any virtual table or virtual pointer.It uses Virtual Table and Virtual Pointer.
(more…)
Early vs Late Binding using Virtual Table and VPtr Read More

Virtual Functions in C++ Explained With Simple Example

A virtual function is a member function which is defined in Base class and redefined by the derived class. “virtual” keyword must be added before the function declaration to define a function virtual. These functions gets inherited by Derived class from Base class and Derived class can choose to provide it’s own definition.
A pure virtual function is a virtual function which has no definition in Base class. Pure virtual function definition is used to ensure all derived class must override the base class function definition.

Normal syntax to define virtual function is as follows:

virtual int samplefun (int x, char c);
(more…)
Virtual Functions in C++ Explained With Simple Example Read More