Posts

Copy constructor in C++ (Part 2)

Now, the important question comes is regarding the signature of copy constructor. That is, why copy constructor takes a reference as parameter. Let's copy by value from previous example. CopyDemo(const CopyDemo obj) {     ptr = new int;     *ptr = *obj.ptr; } Now from main(), CopyDemo o2 = o1; Let me explain in detail. We have called copy constructor to copy o1 to o2. But, since we are passing by value, first o1 has to be copied to obj. So internally, statement will be, const CopyDemo obj = o1; Now, what happens is again copy constructor is invoked. Already, we can sense some problem, copy constructor is called again. Internally, the statement is repeated again. const CopyDemo obj = o1; This goes into a recursive loop and following statements get repeated till stack overflow happens and ultimately program crashes. const CopyDemo obj = o1; const CopyDemo obj = o1; const CopyDemo obj = o1; . . .

Copy constructor in C++ (Part 1)

In C++, the copy constructor is called when the object is not already created. For example, CopyDemo o1, o2; CopyDemo MyObj = o1;  // Here MyObj is created and at the same time initialized. o1 = o2 // Here o1 is already created, so assignment operator function is called. When initializing MyObj, copy constructor is called and when initializing o1, assignment operator function is called. The other scenarios are when we pass object to another function and when we return object from a function. Examples: func(MyObj); MyObj = getObj(); The example code is as below: #include <iostream> using namespace std; class CopyDemo {     public:         CopyDemo(int initValue) {             ptr = new int;             *ptr = initValue;         }         CopyDemo(const CopyDemo &obj) {             cout << "Calling copy constructor\n";             ptr = new int;             *ptr = *obj.ptr;         }     private:         int *ptr; }; int main() {     CopyDemo o1(10);     CopyDem

Object oriented programming (Introduction) - Article 1

Hi Folks, Let's start off on OOP and C++. This is an introductory article for the bigger things coming in the future weeks. What is OOP or Object oriented programming? OOP stands for object oriented programming. C#, C++ and Java are examples of object oriented languages, where as C, Pascal are examples of procedural languages. Modularity, extensibility, reusability and maintainability are some of the advantages of OO languages. In OOP, the software programs are organized as OBJECTs. All the entities in OOP are modelled as objects. Suppose a problem is given, in procedural langauge like C, the problem is divided into functions (actions on data) and data are declared, whereas in C++, the problem is modelled as objects. For example, a problem on car in C will look like, float brake; float acceleration; main() {     accelerate_the_car();         if(obstacle == true)             apply_brake(); } void accelerate_the_car() { } void appl

Synchronization primitives: Mutex and Semaphore (Differences between Mutex and Semaphore) - Article 12

Hi Folks, Sorry, had to take a long break. In this article, let's summarize the differences between Mutex and Semaphore and this is the last article in the series. In the coming articles we shall discuss more about OO, Design, C++, etc and revisit synchronization as and when needed. Mutex Semaphore 1 Mutex is a locking mechanism only to ensure mutual exclusion, that is only one thread or process is allowed to execute the code in critical section. Semaphore can be used for locking, signalling and variety of problems as we have seen in the examples. 2 Mutex is owned and only thread that has locked the mutex can unlock it. Semaphore does not have ownership, anybody can unlock a semaphore. 3 Process wide. System wide. 4 Mutex is used to synchronize threads. Semaphore is used to synchronize threads/processes. 5 Simpler to use. Complex semantics. To

Synchronization primitives: Mutex and Semaphore (Condition Variables) - Article 11

Hi Folks, In previous articles, we have understood the synchronization constructs Mutex and Semaphore. Semaphore is a superior construct compared to a Mutex. Mutex is just a lock where as Semaphore can be used to solve variety of problems. I have explained Mutex & Semaphore using pseudo code and in recent articles, I have provided real PThreads & Mutex code which can be executed & tested in Linux. Going forward, we shall explore Condition Variables & the differences between Mutex & Semaphore. We shall also write some real Semaphore code in Linux. We shall also solve few more synchronization problems & conclude Mutex & Semaphore. In this article, let's explore the concept of Condition Variable. -> Mutex is just a locking mechanism. -> Semaphore can be used as a lock or as a signalling mechanism. Now, how can we use Mutex to solve serialization problems? That's where Condition Variables come into the picture. When we use the combinati

Synchronization primitives: Mutex and Semaphore (Sample Program) - Article 10

Hi Folks, I have run this program on Intel I5 processor running Ubuntu Linux. You can imagine the speed and this program is a very tiny one. But still the output is garbled. |3|The list is full Count: 3 ->|3|The list is full |1|The list is full ->|7||3||3|->->The list is full ->|1||2|The list is full ->->|3||1| ->The list is full The list is full The list is full |7|The list is full |7|The list is full The above is not expected output. You can see that list is not displayed properly. The synchronized version of program is shown below using Mutex variable by name pro(tection). #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<pthread.h> #include<stdio.h> #include<stdlib.h> typedef struct node {     struct node *link;     int data; } NODE; static int count = 0; NODE *head = NULL; pthread_mutex_t pro; NODE *get_node(void) {     int data = rand();     data = data % 13;     NODE *ptr = (NODE *)malloc(s

Synchronization primitives: Mutex and Semaphore (Sample Program) - Article 9

Hi Folks, In this article, I am going to present my version of producer consumer problem which we need to synchronize. The producer can produce a maximum of 5 elements and the consumer consumes 1 element at a time. All data is shared and hence, synchronization is needed. In this article, I am going to present the program. In the next article, we shall apply synchronization. #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<pthread.h> #include<stdio.h> #include<stdlib.h> typedef struct node {     struct node *link;     int data; } NODE; static int count = 0; NODE *head = NULL; NODE *get_node(void) {     int data = rand();     data = data % 13;     NODE *ptr = (NODE *)malloc(sizeof(NODE));     if(ptr != NULL) {         ptr->data = data;         ptr->link = NULL;     }     return ptr; } void print_node(NODE *ptr) {     printf("|%d|",ptr->data);     if(ptr->link != NULL) {