Posts

Showing posts from 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…

Unix - Checking the virtual memory of process

ps -elf | grep

In Solaris the size (SIZ) column specifies memory size

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");
}

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)
}