Tuesday, 22 February 2011

A generic sort program with 'functors' and 'templates'

Picked up this question from here and made a program out of it. It may be a good idea to quickly brush functors and templates if required.

The program sorts the input Vector provided regardless of the type.




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace
std;

class
Int
{

public
:
Int() {x_ = 0;}
Int(const int &x) {x_ = x;}
int
getID (void) {return x_;}
int
x_;
};


class
Str
{

public
:
Str() {x_ = "";}
Str(const string &x) {x_ = x;}
string getID (void) {return x_;}
string x_;
};


template
<typename Object> class Comparator {
public
:
bool
operator()(const Object &o1, const Object &o2) const
{

return
(const_cast<Object&>(o1).getID() < const_cast<Object&>(o2).getID());
}


bool
operator()(const Object *o1, const Object *o2) const {
return
(o1->getID() < o2->getID());
}
};


template
<typename VecObject> void Display(VecObject v)
{

VecObject::iterator it;
for
(it = v.begin(); it != v.end(); ++it)
{

cout<<it->getID()<<", ";
}

cout<<endl;
}


int
main()
{

vector<Int> objects1;
objects1.push_back(Int(3));
objects1.push_back(Int());
objects1.push_back(Int(77));

//print the output
cout<<"objects1 before sort = ";
Display(objects1);
std::sort(objects1.begin(), objects1.end(), Comparator<Int> ());
cout<<"objects1 after sort = ";
Display(objects1);

std::vector<Str> objects2;
objects2.push_back(Str("Hello Hello"));
objects2.push_back(Str("apple?"));
objects2.push_back(Str());
objects2.push_back(Str("1 Jump"));

//print the output
cout<<"objects2 before sort = ";
Display(objects2);
std::sort(objects2.begin(), objects2.end(), Comparator<Str> ());
cout<<"objects2 after sort = ";
Display(objects2);

return
0;
}




The output is as follows:

Tuesday, 15 February 2011

Debugging Mutex and Locks

When there are multiple Mutex's in the program, it may be required to find if a particular thread is locked far longer than necessary. This can cause problems and the output may not be what is expected.

To get round this, I took an old example from here and modified it to help me print some debugging info.

I modified the printSomething() in Singleton.h to add a sleep as follows:



  void printSomething(char *name, int count)
{

Lock guard(mutex_);
Sleep(10);
Lock guard2(mutex_);
std::cout << name << " loop " << count << std::endl;
}




and I modified the Mutex.h as follows:



//Example from http://www.relisoft.com/Win32/active.html
#if !defined _MUTEX_H_
#define _MUTEX_H_

class
Mutex
{

friend class
Lock;
public
:
Mutex () { InitializeCriticalSection (& _critSection); }
~
Mutex () { DeleteCriticalSection (& _critSection); }
private
:
void
Acquire ()
{

DWORD start = GetTickCount();
EnterCriticalSection (& _critSection);
DWORD elapsed = GetTickCount() - start;
if
(elapsed > 15)
{

//Debugging Info
std::cout<<"Debugging Info: Waited at mutex for "<<elapsed<<std::endl;
}
}

void
Release ()
{

LeaveCriticalSection (& _critSection);
}


CRITICAL_SECTION _critSection;
};


#endif



The output is as follows:
The small problem with the above approach is that if a Thread is deadlocked, we may not get the debug output as we would have to kill the process.

Wednesday, 9 February 2011

Add two unsigned integers without using '+'

I found this very interesting discussion (and the program) to add two numbers without the use of the arithmetic operator '+'. The obvious guess would be to use the Bitwise operators.

The logic behind the addition operation using the bitwise operators is as follows:


integer1 =  3  = 0011b
integer2 = 5 = 0101b

first operation second operation third operation
0011 0011
shift by one
0101 0101

______ ______
XOR 0110 AND 0001 ------> <<1 0010

Again...
first operation second operation third operation
previous XOR 0110 0110 shift by one
previous <<1 0010 0010
______ ______
XOR 0100 AND 0010 ------> <<1 0100

Again...
first operation second operation third operation
previous XOR 0100 0100 shift by one
previous <<1 0100 0100
______ ______
XOR 0000 AND 0100 ------> <<1 1000

Again...
first operation second operation
previous XOR 0000 0000
previous <<1 1000 1000
______ ______
XOR 1000 AND 0000
When AND iguals 0 the result of XOR is iqual to the sum of the two integers

The program is as follows:



//Program to add two numbers by using boolean operators
//Ref: http://www.daniweb.com/forums/thread84950.html
//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

unsigned long
add(unsigned integer1, unsigned integer2)
{

unsigned long
xor, and, temp;

and
= integer1 & integer2; /* Obtain the carry bits */
xor
= integer1 ^ integer2; /* resulting bits */

while
(and != 0 ) /* stop when carry bits are gone */
{

and
<<= 1; /* shifting the carry bits one space */
temp = xor ^ and; /* hold the new xor result bits*/
and
&= xor; /* clear the previous carry bits and assign the new carry bits */
xor
= temp; /* resulting bits */
}

return
xor; /* final result */
}


int
main()
{

int
num1 = 13, num2 = 27;

cout << num1 << " + " << num2 << " = " <<add(num1, num2) << endl;

return
0;
}



The output is:
13 + 27 = 40

See the original discussion here.

Wednesday, 2 February 2011

Consequences of Ignoring Compiler Warnings

Though this is a fictional program that I have picked up from here, I have seen similar problems in real life.

Lets say my program (below) was expected to return this


but instead returned:

Program as follows:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

class
Bclass
{

public
:
Bclass() {}
virtual
void func()
{

cout<<"In Bclass::func()"<<endl;
}
};


class
Dclass : public Bclass
{

public
:
Dclass() {}
virtual
void func()
{

cout<<"In Dclass::func()"<<endl;
Bclass:func();
}
};


int
main()
{

Dclass d;
d.func();
//...
return 0;
}



The clue of the problem was given by the compiler that generated the following warning:

main.cpp(23) : warning C4102: 'Bclass' : unreferenced label

pointing to the line

Bclass:func();

The problem lies in the fact that due to a single : rather than :: the line above is being treated as label and since its an unreferenced label it goes back to the start of the same function.

You can read the complete discussion here. Make sure to check the warnings the next time your program behaves unexpectedly.