Tuesday, May 17, 2005

Importance of TypeSafety -An example

class Bank
{

private:
bool bFirstLock;
bool bSecondLock;
BankVault* pBankVault;

public:

Bank()
{
bFirstLock=0;
bSecondLock=0;

pBankVault= new BankVault ;
}

bool IsLocked()
{
if(( true == bFirstLock) && (true== bSecondLock))
{
printf("Both Locks unlocked- Bank is UNLOCKED\n");
return false;
}
else
{
printf("Bank is LOCKED\n");
return true;
}
};


};

//-----------------------------------------------------
class Robber
{
short ForgedKey;
void* pAceesor;

public:

Robber()
{
ForgedKey=0;
};

RemoveLocks()
{
//This method has the instruction to set the first
// 2 bytes (short) of the start of the class to value
//257; This in binary is 00000001 00000001;ie the first 2 bytes to 1 ;
//But since we are using the pointer of the Bank object to call
//this method, it sets the first 2 bytes of the object of
//bank to 00000001 00000001; But in the Bank object the first 2
//bytes are occupied by two boolean variables; Hence this method
//in effect sets the booleans to true

ForgedKey=257;

}
GetCahFromVault()
{
// pAceesor=
}

};
//------------------------------------------------------------

void Run()
{


Bank* pBank = new Bank ;
Robber* pRobber =0;//


pBank->IsLocked(); //?? Output is bank is locked

pRobber= reinterpret_cast (pBank);

pRobber->RemoveLocks();

printf("After wrong type casting\n");

pBank->IsLocked(); //?? Output is bank is UN LOCKED

return;

}

Thursday, February 10, 2005

customising stl find algorithm

customising stl find algorithm

class tTaskType
{

public:
int data;
bool operator == (int k)
{
if(k ==data)
return true;
else
return false;
}
};

---------------------------------
Main()
tTaskType s1,s2,s3;

s1.data =10;
s2.data =12;
s3.data =13;

list test;
list::iterator Itrtest;

test.push_back(s1);
test.push_back(s2);
test.push_back(s3);


Itrtest =std::find(test.begin(),test.end() ,10) ;

if(Itrtest!=test.end() )
{
printf("Found\n");
}

Friday, January 21, 2005

Signalling event

Main thread
{

//Create an eventin the non signalled mode and wait for it
HANDLE g_Event =CreateEvent(0,0,0,0);

//this method will wait till the event is Signalled
WaitForSingleObject(g_Event,INFINITE) ;

}

In another thread

{
//Set the event to signelled state, so that waitforsingle object can return
SetEvent(g_Event)
}

Install/Upgrade NVIDI Driver in Ubuntu for CUDA SDK

Most linux distribution comes with the Nouveau https://nouveau.freedesktop.org/wiki/ display driver configured. If you need to use NVIDIA...