Thursday, August 11, 2011

Aggregration

An aggregation is a specific type of composition where no ownership between the complex object and the subobjects is implied. When an aggregate is destroyed, the subobjects are not destroyed.

For example, consider the math department of a school, which is made up of one or more teachers. Because the department does not own the teachers (they merely work there), the department should be an aggregate. When the department is destroyed, the teachers should still exist independently (they can go get jobs in other departments).

Because aggregations are just a special type of compositions, they are implemented almost identically, and the difference between them is mostly semantic. In a composition, we typically add our subclasses to the composition using either normal variables or pointers where the allocation and deallocation process is handled by the composition class.

In an aggregation, we also add other subclasses to our complex aggregate class as member variables. However, these member variables are typically either references or pointers that are used to point at objects that have been created outside the scope of the class. Consequently, an aggregate class usually either takes the objects it is going to point to as constructor parameters, or it begins empty and the subobjects are added later via access functions or operators.

Because these subclass objects live outside of the scope of the class, when the class is destroyed, the pointer or reference member variable will be destroyed, but the subclass objects themselves will still exist.

Types of Errors

There are two primary types of errors: syntax errors and semantic errors.

A syntax error occurs when you write a statement that is not valid according to the grammar of the C++ language. This happens a lot through typos or accidental omission of keywords or symbols that C++ is expecting. Fortunately, the compiler will generally catch syntax errors and generate warnings or errors so you know what the problem is.

Once your program is compiling correctly, getting it to actually produce the result(s) you want can be tricky. A semantic error occurs when a statement is syntactically valid, but does not do what the programmer intended. Unfortunately, the compiler will not be able to catch these types of problems, because it only knows what you wrote, not what you intended.

Wednesday, June 15, 2011

STL containers?

What are the types of STL containers?

• deque
• hash_map
• hash_multimap
• hash_multiset
• hash_set
• list
• map
• multimap
• multiset
• set
• vector

Sequence containers are: vector deque list
Associative containers are: set multiset map and multimap.
Containers adapters: stack queue and priority_queue.
No hash containers are defined in the current C++ STL standard. However other STL implementation might have some hash based containers (ie STL SGI implementation).

The primary idea in the STL is the container (also known as a collection), which is just what it sounds like: a place to hold things. You need containers because objects are constantly marching in and out of your program and there must be someplace to put them while they’re around. You can’t make named local objects because in a typical program you don’t know How many, or what type, or the lifetime of the objects you’re working with. So you need a container that will expand whenever necessary to fill your needs. All the containers in the STL hold objects and expand themselves. In addition, they hold your objects in a particular way.
A vector is a linear sequence that allows rapid random access to its elements. However, it’s expensive to insert an element in the middle of the sequence, and is also expensive when it allocates additional storage. A deque is also a linear sequence, and it allows random access that’s nearly as fast as vector, but it’s significantly faster when it needs to allocate new storage, and you can easily add new elements at either end (vector only allows the addition of elements at its tail). A list the third type of basic linear sequence, but it’s expensive to move around randomly and cheap to insert an element in the middle. Thus list, deque and vector are very similar in their basic functionality (they all hold linear sequences), but different in the cost of their activities. So for your first shot at a program, you could choose any one, and only experiment with the others if you’re tuning for efficiency.
All three have a member function push_back( ) which you use to insert a new element at the back of the sequence (deque and list also have push_front( )).
An iterator is a class that abstracts the process of moving through a sequence. It allows you to select each element of a sequence without knowing the underlying structure of that sequence.This is a powerful feature, partly because it allows us to learn a single interface that works with all containers, and partly because it allows containers to be used interchangeably.

class Shape {
public:
virtual void draw() = 0;
virtual ~Shape() {};
};
class Circle : public Shape {
public:
void draw() { cout << "Circle::draw\n"; } ~Circle() { cout << "~Circle\n"; } }; class Triangle : public Shape { public: void draw() { cout << "Triangle::draw\n"; } ~Triangle() { cout << "~Triangle\n"; } }; class Square : public Shape { public: void draw() { cout << "Square::draw\n"; } ~Square() { cout << "~Square\n"; } }; typedef std::vector Container;
typedef Container::iterator Iter;
int main() {
Container shapes;
shapes.push_back(new Circle);
shapes.push_back(new Square);
shapes.push_back(new Triangle);
for(Iter i = shapes.begin();
i != shapes.end(); i++)
(*i)->draw();
// ... Sometime later:
for(Iter j = shapes.begin();
j != shapes.end(); j++)
delete *j;
} ///:~

Tuesday, April 19, 2011

Shallow vs. deep copying

Shallow copying
Because C++ does not know much about your class, the default copy constructor and default assignment operators it provides use a copying method known as a shallow copy (also known as a memberwise copy). A shallow copy means that C++ copies each member of the class individually using the assignment operator. When classes are simple (eg. do not contain any dynamically allocated memory), this works very well.
For example, let’s take a look at our Cents class:

class Cents {
private:
int m_nCents;
public:
Cents(int nCents=0)
{
m_nCents = nCents;
}
};
When C++ does a shallow copy of this class, it will copy m_nCents using the standard integer assignment operator. Since this is exactly what we’d be doing anyway if we wrote our own copy constructor or overloaded assignment operator, there’s really no reason to write our own version of these functions!
However, when designing classes that handle dynamically allocated memory, memberwise (shallow) copying can get us in a lot of trouble! This is because the standard pointer assignment operator just copies the address of the pointer — it does not allocate any memory or copy the contents being pointed to!
class MyString
{
private:
char *m_pchString;
int m_nLength;
public:
MyString(char *pchString="")
{
// Find the length of the string
// Plus one character for a terminator
m_nLength = strlen(pchString) + 1;
// Allocate a buffer equal to this length
m_pchString= new char[m_nLength];
// Copy the parameter into our internal buffer
strncpy(m_pchString, pchString, m_nLength);
// Make sure the string is terminated
m_pchString[m_nLength-1] = '\\0';
}


~MyString() // destructor
{
// We need to deallocate our buffer
delete[] m_pchString;
// Set m_pchString to null just in case
m_pchString = 0;
}
char* GetString() { return m_pchString; }
int GetLength() { return m_nLength; }
};
The above is a simple string class that allocates memory to hold a string that we pass in. Note that we have not defined a copy constructor or overloaded assignment operator. Consequently, C++ will provide a default copy constructor and default assignment operator that do a shallow copy.
MyString cHello("Hello, world!");
{
MyString cCopy = cHello; // use default copy constructor
} // cCopy goes out of scope here
std::cout << cHello.GetString() << std::endl; // this will crash While this code looks harmless enough, it contains an insidious problem that will cause the program to crash! Can you spot it? Don’t worry if you can’t, it’s rather subtle. Let’s break down this example line by line: MyString cHello("Hello, world!"); This line is harmless enough. This calls the MyString constructor, which allocates some memory, sets cHello.m_pchString to point to it, and then copies the string “Hello, world!” into it. MyString cCopy = cHello; // use default copy constructor This line seems harmless enough as well, but it’s actually the source of our problem! When this line is evaluated, C++ will use the default copy constructor (because we haven’t provided our own), which does a shallow pointer copy on cHello.m_pchString. Because a shallow pointer copy just copies the address of the pointer, the address of cHello.m_pchString is copied into cCopy.m_pchString. As a result, cCopy.m_pchString and cHello.m_pchString are now both pointing to the same piece of memory! } // cCopy goes out of scope here Now you can see why this crashes. We deleted the string that cHello was pointing to, and now we are trying to print the value of memory that is no longer allocated. The root of this problem is the shallow copy done by the copy constructor — doing a shallow copy on pointer values in a copy constructor or overloaded assignment operator is almost always asking for trouble. Deep copying
The answer to this problem is to do a deep copy on any non-null pointers being copied. A deep copy duplicates the object or variable being pointed to so that the destination (the object being assigned to) receives it’s own local copy. This way, the destination can do whatever it wants to it’s local copy and the object that was copied from will not be affected. Doing deep copies requires that we write our own copy constructors and overloaded assignment operators.
Let’s go ahead and show how this is done for our MyString class:
// Copy constructor
MyString::MyString(const MyString& cSource)
{
// because m_nLength is not a pointer, we can shallow copy it
m_nLength = cSource.m_nLength;
// m_pchString is a pointer, so we need to deep copy it if it is non-null
if (cSource.m_pchString)
{
// allocate memory for our copy
m_pchString = new char[m_nLength];
// Copy the string into our newly allocated memory
strncpy(m_pchString, cSource.m_pchString, m_nLength);
}
else
m_pchString = 0;
}

As you can see, this is quite a bit more involved than a simple shallow copy! First, we have to check to make sure cSource even has a string (line 8). If it does, then we allocate enough memory to hold a copy of that string (line 11). Finally, we have to manually copy the string using strncpy() (line 14).
Now let’s do the overloaded assignment operator. The overloaded assignment operator is a tad bit trickier:
// Assignment operator
MyString& MyString::operator=(const MyString& cSource)
{
// check for self-assignment
if (this == &cSource)
return *this;
// first we need to deallocate any value that this string is holding!
delete[] m_pchString;
// because m_nLength is not a pointer, we can shallow copy it
m_nLength = cSource.m_nLength;
// now we need to deep copy m_pchString
if (cSource.m_pchString)
{
// allocate memory for our copy
m_pchString = new char[m_nLength];
// Copy the parameter the newly allocated memory
strncpy(m_pchString, cSource.m_pchString, m_nLength);
}
else
m_pchString = 0;
return *this;
}
Note that our assignment operator is very similar to our copy constructor, but there are three major differences:
• We added a self-assignment check (line 5).
• We return *this so we can chain the assignment operator (line 26).
• We need to explicitly deallocate any value that the string is already holding (line 9).
When the overloaded assignment operator is called, the item being assigned to may already contain a previous value, which we need to make sure we clean up before we assign memory for new values. For non-dynamically allocated variables (which are a fixed size), we don’t have to bother because the new value just overwrite the old one. However, for dynamically allocated variables, we need to explicitly deallocate any old memory before we allocate any new memory. If we don’t, the code will not crash, but we will have a memory leak that will eat away our free memory every time we do an assignment!

Checking for self-assignment
In our overloaded assignment operators, the first thing we do is check for self assignment. There are two reasons for this. One is simple efficiency: if we don’t need to make a copy, why make one? The second reason is because not checking for self-assignment when doing a deep copy will cause problems if the class uses dynamically allocated memory. Let’s take a look at an example of this.
Consider the following overloaded assignment operator that does not do a self-assignment check:
// Problematic assignment operator
MyString& MyString::operator=(const MyString& cSource)
{
// Note: No check for self-assignment!
// first we need to deallocate any value that this string is holding!
delete[] m_pchString;
// because m_nLength is not a pointer, we can shallow copy it
m_nLength = cSource.m_nLength;
// now we need to deep copy m_pchString
if (cSource.m_pchString)
{
// allocate memory for our copy
m_pchString = new char[m_nLength];
// Copy the parameter the newly allocated memory
strncpy(m_pchString, cSource.m_pchString, m_nLength);
}
else
m_pchString = 0;
return *this;
}

What happens when we do the following?
cHello = cHello;
This statement will call our overloaded assignment operator. The this pointer will point to the address of cHello (because it’s the left operand), and cSource will be a reference to cHello (because it’s the right operand). Consequently, m_pchString is the same as cSource.m_pchString.
Now look at the first line of code that would be executed: delete[] m_pchString;.

This line is meant to deallocate any previously allocated memory in cHello so we can copy the new string from the source without a memory leak. However, in this case, when we delete m_pchString, we also delete cSource.m_pchString! We’ve now destroyed our source string, and have lost the information we wanted to copy in the first place. The rest of the code will allocate a new string, then copy the uninitialized garbage in that string to itself. As a final result, you will end up with a new string of the correct length that contains garbage characters.
The self-assignment check prevents this from happening.
Preventing copying

Sometimes we simply don’t want our classes to be copied at all. The best way to do this is to add the prototypes for the copy constructor and overloaded operator= to the private section of your class.
class MyString
{
private:
char *m_pchString;
int m_nLength;
MyString(const MyString& cSource);
MyString& operator=(const MyString& cSource);
public:
// Rest of code here
};

In this case, C++ will not automatically create a default copy constructor and default assignment operator, because we’ve told the compiler we’re defining our own functions. Furthermore, any code located outside the class will not be able to access these functions because they’re private.
Summary
•The default copy constructor and default assignment operators do shallow copies, which is fine for classes that contain no dynamically allocated variables.
•Classes with dynamically allocated variables need to have a copy constructor and assignment operator that do a deep copy.
•The assignment operator is usually implemented using the same code as the copy constructor, but it checks for self-assignment, returns *this, and deallocates any previously allocated memory before deep copying.
•If you don’t want a class to be copy able, use a private copy constructor and assignment operator prototype in the class header.

Straight through processing

Straight through processing (STP) is the end-to-end automation of the trading processes both within and between buy and sell side institutions. In short, it is a vehicle to real-time stock/ trade processing in Financial Service industry, with a seamless integration of components and processes involved in the trading cycle starting from first request to buy/sell interest ending up to trading settlement and reporting.

It starts from the first capture of an order through to final settlement. It involves the seamless, electronic transfer of information to all parties involved in the trading cycle utilizing standardized information flows, technologies, and infrastructures.

STP provides wired links for investment managers and ability to process trade without manual intervention and exception, thereby eliminating chances for human errors.

Advantages
The present trade lifecycle is a maze of manual and electronic processes, taking several days, typically three to five days, from initiation to settlement. STP does it all electronically without the need for re-keying or manual intervention. Today, when an investor (individual or corporate) makes a trade, the order starts a complex procedure that extends over a few days. Phone calls and other paper documents fly back and forth between various players like broker/dealers, asset managers, etc. This complex set of operations is true even for a relatively simple domestic retail equity order. The complexity increases for a cross - border trade before it’s finalized.

It is a known fact that as trading volumes explodes, failure rates increase, which in turn, degrades the quality of customer service. To a customer, the ability to achieve a fully integrated STP capability enables greater access to liquidity with a service linking all the areas of the investment chain. STP, in its entirety, is expected to provide all affected financial services players with tremendous benefits, including greatly shortened processing cycles, reduced risk and lower operating costs. In addition, STP reduces errors through lost or wrongly input orders, speeding settlement, reducing risk, and cost of capital.

One of the key drivers of this interest was the T+1 initiative, originally conceived by SIA, the American Securities Industry forum, to address potentially increasing trading volumes in the US securities market. It was anticipate that, STP solutions would be needed to meet the global demand that has resulted from the explosive growth of online trading.

Historically, STP solutions were needed to help financial markets firms move to one-day trade settlement of equity transactions, as well as to meet the global demand resulting from the explosive growth of online trading. Now the concepts of STP are applied to reduce systemic and operational risk and to improve certainty of settlement and minimize operational costs.

When fully realized, STP provides asset managers, broker/dealers, custodians, banks and other financial services players with tremendous benefits, including greatly shortened processing cycles, reduced settlement risk and lower operating costs. Some industry analysts believe that STP is not an achievable goal in the sense that firms are unlikely to find the cost/benefit to reach 100% automation. Instead they promote the idea of improving levels of internal STP within a firm while encouraging groups of firms to work together to improve the quality of the automation of transaction information between themselves, either bilaterally or as a community of users (external STP). Other analysts, however, believe that STP will be achieved with the emergence of business process interoperability.


Impacted players:
Every player in the trade cycle will be affected in this process. Investors (Retail customers), Fund Managers, Broker/ Dealer, Custodian, Clearing Agents, Stock Exchange, Investment Managers, Credit rating agencies, Electronic Transaction Network, Information Providers (Electronic & others), Regulatory Bodies, Vendors are a few of the chain of players who would definitely be affected.
In summary
STP is a critical competitive weapon in the financial services industry. The key driver has always been business needs and therefore firms should not view STP narrowly as an IT solution. Its influence stretches far beyond the trading realm of operations and, when intelligently applied, STP will have a favourable impact on costs as well as the top line.

International Financial Reporting Standards

International Accounting Standards (IAS), now renamed International Financial Reporting Standards (IFRS), are gaining acceptance worldwide.
International Financial Reporting Standards (IFRS) are principles-based Standards, Interpretations and the Framework (1989) adopted by the International Accounting Standards Board (IASB).
Countries that have Adopted IFRS

Africa:
Botswana, Egypt, Ghana, Kenya, Malawi, Mauritius, Mozambique, Namibia, South Africa, Tanzania
Americas:
Bahamas, Barbados, Brazil (2010), Canada (2011), Chile (2009), Costa Rica, Dominican Republic, Ecuador, Guatemala, Guyana, Haiti, Honduras, Jamaica, Nicaragua, Panama, Peru, Trinidad and Tobago, Uruguay, Venezuela
Asia:
Armenia, Bahrain, Bangladesh, Georgia, Hong Kong, India (2011), Israel, Jordan, Kazakhstan, Kuwait, Kyrgyzstan, Lebanon, Nepal, Oman, Philippines, Qatar, Singapore, South Korea (2011), Sri Lanka (2011), Tajikistan, United Arab Emirates
Europe:
Austria, Belarus, Belgium, Bosnia and Herzegovina, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark, Estonia, Finland, France, Germany, Greece, Hungary, Iceland, Ireland, Italy, Latvia, Liechtenstein, Lithuania, Luxembourg, Macedonia, Malta, Montenegro, Netherlands, Norway, Poland, Portugal, Romania, Russia, Serbia, Slovakia, Slovenia, Spain, Sweden, Turkey, Ukraine, United Kingdom
Oceania:
Australia, Fiji, New Zealand, Papua New Guinea
Components of IFRS financial statements:
Formats-To show specific items on the face of the primary IFRS financial statements.

Compliance with IFRS-Financial Statements complying with the IFRS should disclose the fact
For First time adoption of IFRS-The entity adopting IFRS for the first time should prepare financial statements(including comparatives) at the reporting date for the first IFRS financial statements.
The set of financial statements under IFRS

• Accounting Policies
• Statement of Comprehensive Income
• Balance Sheet
• Cash Flow Statement
• Statement of changes in Equity
• Notes on Accounts Need for IFRS
• Level of confidence: The key benefit will be common accounting system that is perceived as stable, transperant and fair to investors across the world.
• Risk Evaluation: IFRS will eliminate the barriers to cross-border listings and will be beneficial for investors who generally ascribe a risk premium if the underlying financial information is not prepared in accordance with international standards
• Merger and Takeover Activity: Cross-border mergers and acquisitions will get a boost by making it easier for the parties involved in as far as redrawing the financial statements is concerned.
• Investments: Foreign investors will be attracted to economies where IFRS-complaint financial statements are the norm.
Expected Benefits
(1) More efficient formulation of domestic accounting standards, improvement of their international image, and enhancement of the global rankings and international competitiveness of our local capital markets;
(2) Better comparability between the financial statements of local and foreign companies;
(3) No need for restatement of financial statements when local companies wish to issue overseas securities, resulting in reduction in the cost of raising capital overseas;
(4) For local companies with investments overseas, use of a single set of accounting standards will reduce the cost of account conversions and improve management efficiency.

Information Source:
http://en.wikipedia.org/wiki/International_Financial_Reporting_Standards
http://catuts.com/ifrs-introduction/
http://www.ftkmc.com/newsletter/Vol1-3-apr5-2010.pdf

Return by address

Returning by address involves returning the address of a variable to the caller. Just like pass by address, return by address can only return the address of a variable, not a literal or an expression. Like return by reference, return by address is fast. However, as with return by reference, return by address cannot return local variables:
int* DoubleValue(int nX)
{
int nValue = nX * 2;
return &nValue; // return nValue by address here
}

As you can see here, nValue goes out of scope just after its address is returned to the caller. The end result is that the caller ends up with the address of non-allocated memory, which will cause lots of problems if used. This is one of the most common programming mistakes that new programmers make. Many newer compilers will give a warning (not an error) if the programmer tries to return a local variable by address — however, there are quite a few ways to trick the compiler into letting you do something illegal without generating a warning, so the burden is on the programmer to ensure the address they are returning will be to a valid variable after the function returns.
Return by address is often used to return newly allocated memory to the caller:

int* AllocateArray(int nSize)
{
return new int[nSize];
}
int main()
{
int *pnArray = AllocateArray(25);
// do stuff with pnArray
delete[] pnArray;
return 0;
}

Return by reference

Return by reference
Just like with pass by reference, values returned by reference must be variables (you can not return a reference to a literal or an expression). When a variable is returned by reference, a reference to the variable is passed back to the caller. The caller can then use this reference to continue modifying the variable, which can be useful at times. Return by reference is also fast, which can be useful when returning structs and classes.
However, returning by reference has one additional downside that pass by reference doesn’t — you cannot return local variables to the function by reference.
Consider the following Example:

int& DoubleValue(int nX)
{
int nValue = nX * 2;
return nValue; // return a reference to nValue here
}

See the problem here? The function is trying to return a reference to a value that is going to go out of scope when the function returns. This would mean the caller receives a reference to garbage. Fortunately, your compiler will give you an error if you try to do this.
Return by reference is typically used to return arguments passed by reference to the function back to the caller. In the following example, we return (by reference) an element of an array that was passed to our function by reference:

struct FixedArray25
{
int anValue[25];
};
// Returns a reference to the nIndex element of rArray
int& Value(FixedArray25 &rArray, int nIndex)
{
return rArray.anValue[nIndex];
}
int main()
{
FixedArray25 sMyArray;
// Set the 10th element of sMyArray to the value 5
Value(sMyArray, 10) = 5;
cout << sMyArray.anValue[10] << endl; return 0; } Output : 5


When we call Value(sMyArray, 10), Value() returns a reference to the 10th element of the array inside sMyArray. main() then uses this reference to assign that element the value 5.

Although this is somewhat of a contrived example (because you could access sMyArray.anValue directly), once you learn about classes you will find a lot more uses for returning values by reference.

Return by value

Return by value is the simplest and safest return type to use. When a value is returned by value, a copy of that value is returned to the caller. As with pass by value, you can return by value literals (eg. 5), variables (eg. x), or expressions (eg. x+1), which makes return by value very flexible.

Advantage of return by value is that you can return variables (or expressions) that involve local variables declared within the function. Because the variables are evaluated before the function goes out of scope, and a copy of the value is returned to the caller, there are no problems when the variable goes out of scope at the end of the function.

int DoubleValue(int nX)
{
int nValue = nX * 2;
return nValue; // A copy of nValue will be returned here
}

Return by value is the most appropriate when returning variables that were declared inside the function, or for returning function arguments that were passed by value. However, like pass by value, return by value is slow for structs and large classes.

virtual functions

To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

Every class that uses virtual functions (or is derived from a class that uses virtual functions) is given its own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.
Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically) when a class instance is created so that it points to the virtual table for that class. Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer. It also means that *__vptr is inherited by derived classes, which is important.
Example
class Base
{
public:
virtual void function1() {};
virtual void function2() {};
};
class D1: public Base
{
public:
virtual void function1() {};
};
class D2: public Base
{
public:
virtual void function2() {};
};
There are 3 classes here, the compiler will set up 3 virtual tables: one for Base, one for D1, and one for D2.
The compiler also adds a hidden pointer to the most base class that uses virtual functions. Although the compiler does this automatically.

class Base
{
public:
FunctionPointer *__vptr;
virtual void function1() {};
virtual void function2() {};
};

class D1: public Base
{
public:
virtual void function1() {};
};
class D2: public Base
{
public:
virtual void function2() {};
};

When a class object is created, *__vptr is set to point to the virtual table for that class. For example, when an object of type Base is created, *__vptr is set to point to the virtual table for Base. When objects of type D1 or D2 are constructed, *__vptr is set to point to the virtual table for D1 or D2 respectively.

How these virtual tables are filled out. Because there are only two virtual functions here, each virtual table will have two entries (one for function1(), and one for function2()). Remember that when these virtual tables are filled out, each entry is filled out with the most-derived function an object of that class type can call.
Base’s virtual table is simple. An object of type Base can only access the members of Base. Base has no access to D1 or D2 functions. Consequently, the entry for function1 points to Base::function1(), and the entry for function2 points to Base::function2().
D1′s virtual table is slightly more complex. An object of type D1 can access members of both D1 and Base. However, D1 has overridden function1(), making D1::function1() more derived than Base::function1(). Consequently, the entry for function1 points to D1::function1(). D1 hasn’t overridden function2(), so the entry for function2 will point to Base::function2().
D2′s virtual table is similar to D1, except the entry for function1 points to Base::function1(), and the entry for function2 points to D2::function2().


The *__vptr in each class points to the virtual table for that class. The entries in the virtual table point to the most-derived version of the function objects of that class are allowed to call.
So consider what happens when we create an object of type D1:
int main()
{
D1 cClass;
}
Because cClass is a D1 object, cClass has it’s *__vptr set to the D1 virtual table.
Now, let’s set a base pointer to D1:

int main()
{
D1 cClass;
Base *pClass = &cClass;
}
Note that because pClass is a base pointer, it only points to the Base portion of cClass. However, also note that *__vptr is in the Base portion of the class, so pClass has access to this pointer. Finally, note that pClass->__vptr points to the D1 virtual table! Consequently, even though pClass is of type Base, it still has access to D1′s virtual table.

So what happens when we try to call pClass->function1()?
int main()
{
D1 cClass;
Base *pClass = &cClass;
pClass->function1();
}
First, the program recognizes that function1() is a virtual function. Second, uses pClass->__vptr to get to D1′s virtual table. Third, it looks up which version of function1() to call in D1′s virtual table. This has been set to D1::function1(). Therefore, pClass->function1() resolves to D1::function1()!
Now, you might be saying, “But what if Base really pointed to a Base object instead of a D1 object. Would it still call D1::function1()?”. The answer is no.

int main()
{
Base cClass;
Base *pClass = &cClass;
pClass->function1();
}

In this case, when cClass is created, __vptr points to Base’s virtual table, not D1′s virtual table. Consequently, pClass->__vptr will also be pointing to Base’s virtual table. Base’s virtual table entry for function1() points to Base::function1(). Thus, pClass->function1() resolves to Base::function1(), which is the most-derived version of function1() that a Base object should be able to call.

By using these tables, the compiler and program are able to ensure function calls resolve to the appropriate virtual function, even if you’re only using a pointer or reference to a base class!

Calling a virtual function is slower than calling a non-virtual function for a couple of reasons: First, we have to use the *__vptr to get to the appropriate virtual table. Second, we have to index the virtual table to find the correct function to call. Only then can we call the function. As a result, we have to do 3 operations to find the function to call, as opposed to 2 operations for a normal indirect function call, or one operation for a direct function call. However, with modern computers, this added time is usually fairly insignificant.

Late Binding

Late Binding
In C++, one way to get late binding is to use function pointers. To review function pointers briefly, a function pointer is a type of pointer that points to a function instead of a variable. The function that a function pointer points to can be called by using the function call operator (()) on the pointer.

For example, the following code calls the Add() function
int Add(int nX, int nY)
{
return nX + nY;
}
int main()
{
// Create a function pointer and make it point to the Add function
int (*pFcn)(int, int) = Add;
cout << pFcn(5, 3) << endl; // add 5 + 3 return 0; } Calling a function via a function pointer is also known as an indirect function call.

int Add(int nX, int nY)
{
return nX + nY;
}
int Subtract(int nX, int nY)
{
return nX - nY;
}

int Multiply(int nX, int nY)
{
return nX * nY;
}
int main()
{
int nX;
cout << "Enter a number: "; cin >> nX;
int nY;
cout << "Enter another number: "; cin >> nY;
int nOperation;


do
{
cout << "Enter an operation (0=add, 1=subtract, 2=multiply): "; cin >> nOperation;
} while (nOperation < 0 || nOperation > 2);
int (*pFcn)(int, int);
// Set pFcn to point to the function the user chose
switch (nOperation)
{
case 0: pFcn = Add; break;
case 1: pFcn = Subtract; break;
case 2: pFcn = Multiply; break;
}

cout << "The answer is: " << pFcn(nX, nY) << endl;
return 0;
}

Instead of calling the Add(), Subtract(), or Multiply() function directly, we’ve instead set pFcn to point at the function we wish to call. Then we call the function through the pointer. The compiler is unable to use early binding to resolve the function call pFcn(nX, nY) because it can not tell which function pFcn will be pointing to at compile time!
Late binding is slightly less efficient since it involves an extra level of indirection. With early binding, the compiler can tell the CPU to jump directly to the function’s address. With late binding, the program has to read the address held in the pointer and then jump to that address. This involves one extra step, making it slightly slower. However, the advantage of late binding is that it is more flexible than early binding, because decisions about what function to call do not need to be made until run time.

Thursday, April 7, 2011

Introduction to Hedge Funds

What is a Hedge Fund?
A hedge fund is a loosely regulated private investment fund that charges a management and performance fee.
A hedge fund collects its funds from private wealthy individuals and large institutions and uses funds to trade securities with the hope of capital and income appreciation. Unlike other investment vehicles, hedge funds concentrate on making a consistent absolute return rather than a relative return to a benchmark index. One important distinguishing factor a hedge fund has compared to traditional mutual funds and asset management companies is that a hedge fund is allowed to use almost any structured product. This means that they are allowed to engage in leveraged derivative positions as well as shorting securities, a method usually forbidden at mutual funds. Hedge funds traditionally identify inefficiencies in the financial markets and trade to capture profits from them.
Growth of Hedge Funds
In the last 9 years hedge funds have grown in number by approximately 20% every year.
Currently there are an estimated 9000 hedge funds in the world managing over 1.1 trillion USD. The assets within each hedge fund are also growing every year. Performance is said to account for a third of this increase.
Hedge funds are said to account for around 50 billion USD of fees on Wall Street and City investment banks. They make up more than 50% of US bonds trading, 40% of equity trades and over three quarters of distressed debt trading (explanations of these terms come at a later section).
Laws Relating to Hedge Funds
Hedge funds by all reasonable definitions fall under the definition of an investment company
This means that according to the Investment Company Act of 1940 they need to be registered with the SEC as an investment advisor. There are two exemptions which hedge funds elect however. The first one is under Section 3(c) (1) and the other under Section 3 (c) (7). A 3 (c) 1 fund is simply one that has no more than 100 investors and is not making or planning to make a public offering of its securities. A 3(c) (7) fund is one where the investors of the fund are at the time of the acquisition “qualified purchasers”, meaning that they have more than 5 million USD of assets.
Given these two restrictions the hedge fund can sell its “shares” under what is called Regulation D. Regulation D allows companies to sell shares of the company under private placements. This is a direct private offering of securities to a limited number of sophisticated investors. This is unlike a public offering of stock which can be even sold to the retail. A company can issue shares and sell it as a private placement if it wants to avoid dealing with all the legal mess and financial costs of investment banking fees and registration. The drawback is however that they can only sell them to limited investors with a net worth of at least 1 million USD or minimum income of 200’000 USD in each year in the past two years and expecting it to continue in the future. Given these exemptions a hedge fund can sell itself to wealthy individuals and institutions without the need to register.

Hedge Funds in India
An interesting link to track progress about Hedge Funds in India : http://www.hedgefundsindia.com/
HFG India Continuum Fund
Hudson Fairfax Group (HFG) is an investment partnership focused on India’s aerospace, defense, homeland security and other strategic sectors. It is based in New York with an advisory office in New Delhi. Its team has five decades of focused experience in the sector combining investment and industry expertise. Hudson Fairfax Group, through its predecessor company, started as an investment advisory firm in 2005. It ran an investment fund, the HFG India Continuum Fund, which invested in publicly traded Indian securities. During the operation of its fund, HFG was a Registered Investment Advisor (RIA) with the U.S. Securities & Exchange Commission and a Foreign Institutional Investor (FII) with the Securities & Exchange Board of India.
Avatar Investment Management
Avatar Investment Management is the investment advisor to three funds. Headquartered in Mauritius, the funds are focussed on the Indian public and private equity markets. In order to meet the approval of various regulatory bodies around the world, only accredited investors may apply to invest.
India Deep Value Fund
India Investment Advisors, LLC was founded by Robin Rodriguez and Raj Agarwal in 2006 to pursue the number of significant investment opportunities presented by the burgeoning Indian capital and real estate markets. As a result, the India Deep Value Fund was launched in April 2006. The Fund's Managers seek to achieve long-term capital gains by acting as pro-active deep value investors in publicly-traded Indian stocks.
Fair value
Fair Value Capital is a highly specialized and exclusive Investment Advisory Firm focused on Deep Value Investment opportunities primarily in Indian equity markets. It seek absolute, long-term returns for its investments while minimizing investment risks using a Value oriented approach towards our investments. Fair Value specializes in Deep Value Investments in the Indian equity markets.
Indea Capital Pte Ltd
Indea Capital Pte. Ltd (Indea) is a Singapore based investment advisor. Indea was formed in 2002 to provide boutique fund management services to institutions, foundations, family offices and high net-worth individuals. In July 2003, Indea launched the Indea Absolute Return Fund (IARF), a directional fund investing in India and Indian companies globally. The principals have a combined over 30 years of experience in researching and investing in India. In addition to the Singapore office, Indea has a research presence in Mumbai, India.

Tuesday, April 5, 2011

American and European Options

The most basic difference between an American option and a European option is that a European option may only be exercised on the expiration date, while an American option may be exercised at any point before that date. All options give the holder the option, but not the obligation, to buy (in the case of a call) or sell (in the case of a put).

All options on individual stocks are American style. Options on the major indexes (OEX is an exception) are European. Some examples of actively traded European style options:

SPX: Standard & Poor’s 500 Index
DJX: Dow Jones
Industrial Average
NDX: NASDAQ 100 Index
RUT: Russell 2000 (small cap) Index

Differences in Value

Typically, because an option’s value is based largely on the amount of time left until expiration, the holder of an option usually prefers to close the option position rather than exercise the option. There are rare instances, however, when the owner of an American option will prefer to exercise the option rather than close the position.

For example, just before the underlying stock pays a dividend, if the option’s value falls by more than the remaining value, it is advantageous to exercise the option. Because of the anomalous instances, calculating the value of an American option can be difficult.

Essentially, while a European option can be accurately valued using the famous Black-Scholes pricing model, American options require a more complex pricing model. What most traders need to understand is that an American option will always be worth at least what a European option is worth.

**Asian Options**

An Asian option (or average value option) is a special type of option contract. For Asian options the payoff is determined by the average underlying price over some pre-set period of time. This is different to the case of the usual European option and American option, where the payoff of the option contract depends on the price of the underlying instrument at maturity; Asian options are thus one of the basic forms of exotic options.

One advantage of Asian options is that these reduce the risk of market manipulation of the underlying instrument at maturity. Another advantage of Asian options involves the relative cost of Asian options compared to European or American options. Because of the averaging feature, Asian options reduce the volatility inherent in the option; therefore, Asian options are typically cheaper than European or American options. This can be an advantage for corporations that are subject to the FASB revised Statement No. 123, which requires that corporations expense employee stock options.

Clean Shell/Public Shell

Clean Shell/Public Shell

A reporting company. It has its filings current. It doesn't have debt. There are no lawsuits, nor reasonable prospects of a lawsuit against the company. The insiders retain control of 80%-90% of the issued shares.

Public shell is a viable alternative to going public. In more formal circles they can be referred to as public shell company or public shell corporation.

Public shell transactions are a widely accepted, alternative mean for a private company to go public. A necessary component to a completed reverse merger transaction is the public shell. The public shell is a publicly listed company with no assets or liabilities. It is called a "shell" considering all that exists of the original company is its corporate shell structure. By merging into such an entity, a private company becomes public.

The primary benefits of doing a Public shell, as opposed to an IPO, is the following:

• You will receive a higher valuation for your company.
• The company does not require an underwriter.
• The costs are significantly less than the costs required for an initial public offering.
• The time required is considerably less than for an IPO.
• IPOs generally require greater attention from top management.
• There is less dilution of ownership control.
• While an IPO requires a relatively long and stable earning history, the lack of an earning history does not normally keep a privately-held company from completing a reverse merger.

The fees associated with a standard IPO are also extremely high. The company must pay for underwriting fees, legal fees, accounting fees, printing costs, and filing fees. With a public shell merger, the costs are much less to go public.

Accrued Interest

A term used to describe an accrual accounting method when interest that is either payable or receivable has been recognized, but not yet paid or received. Accrued interest occurs as a result of the difference in timing of cash flows and the measurement of these cash flows.
The interest that has accumulated on a bond since the last interest payment up to, but not including, the settlement date.
The process of calculating the amount of interest accrued depends on identifying the number of days that have passed since the last disbursement of accrued interest to the owner of the bond. At the same time, it is important to know the rate of interest that is compounded at each schedule coupon date.

Accrued Interest = Interest payment * Number of days since last payment /Number of days between payments

Example:
Calculating the Purchase Price for a Bond with Accrued Interest
You purchase a corporate bond with a settlement date on September 15 with a face value of $1,000 and a nominal yield of 8%, that has a listed price of 100-08, and that pays interest semi-annually on February 15 and August 15. How much must you pay?
The semi-annual interest payment is $40 and there were 31 days since the last interest payment on August 15. If the settlement date fell on a interest payment date, the bond price would equal the listed price: 100.25% x $1,000.00 = $1,002.50 (8/32 = 1/4 = .25, so 100-08 = 100.25% of par value). Since the settlement date was 31 days after the last payment date, accrued interest must be added. Using the above formula, with 184 days between coupon payments, the actual purchase price for the bond will be $1,002.50 + $6.74 = $1,009.24

Dynamics in payment processing for OTC derivatives trades

Introduction:

Payment processing is a critical function within the lifecycle of OTC trades. The majority of trades in the $465 trillion OTC derivative trading market are cash-settled. Too often settlement breaks in OTC trades occur as a direct result of mismatching cash flows between the receiving and paying entities on a bilateral agreement. A settlement break directly impacts books and records of a firm. In the new era of post regulation, with a mix of OTC cleared and non-cleared trades, as well as the increase in the number of bilateral contracts using cash as collateral, payment processing will emerge as one of the key operational areas for process efficiency.

However, if both parties establish an agreement on the payment amount to be settled, then they are ensured fixed trade economic details. Thus, a settled payment exchange between the two parties guarantees that both are capable of meeting their trade obligations.

Current payment process for OTC trades:

A payment transaction occurs during an OTC trade’s lifecycle process for the exchange of regular periodic cash flow, coupon payment, upfront payment, termination and innovation fees, credit events; rate reset fees, margin, collateral movement (including interest on collateral), claims and charges. Similar to the OTC trade confirmation process, every payment between the two counterparties is agreed, confirmed and matched before the payment value date. The bilateral netting process reduces the number of cash movements and lowers the cost of payment processing. To achieve the benefits of netting, a single payment instruction is linked to multiple underlying trade details. Firms’ internal systems record the payment history for OTC trades.

In addition, to store the golden and copper record of a credit default swap trade, the Depository Trust & Clearing Corporation Trade Information Warehouse (DTCC TIW) also processes payments for credit derivatives trades via a link with CLS Bank for settlements. Also the new DTCC cash flow matching system for equity derivatives will streamline the payment notices affirmation and confirmation process for OTC equity derivatives. Both initiatives support the industry drive towards standardization and transparency.

The fact that settlement matching and exception payment processing is still the least automated function in an OTC life cycle process highlights the multiple issues that exist within the OTC settlement function. Processing structured and collateralized debt securities has proved challenging ever since their inception in the market. Some of the payment processing issues facing firms include:

• Non standardization of collateralized-based asset types which, with their unique life cycle events, create payment processing inaccuracies
• Late and inaccurate notifications of payment reset rates for CMO’s, CLO’s and ABS Asset Types
• Higher payment processing failure rates due to a steep decline in the value of the assets
• Numerous post-value date adjustments, causing higher risk rates with lower efficiency returns
• Reversal of additional or initial payment due to change in reset rates

Account setup

To streamline regular payment processes, firms maintain standard payment instruction details for each currency, counterparty, account and payment type. This information is exchanged between counterparties on a regular basis usually by fax or email. Firms employ dedicated teams to set up new accounts for counterparties as well as to maintain all static data. When information is incorrect and/or must be changed, the manual process causes delays. A time delay occurs in verification of the changes and updates across all relevant systems, as well as across multiple payment processing platforms. There is no centralized platform or standard messaging to update the standard settlement instruction between the dealers, buy-side firms and custodian.

Payment notices

Payment notices are sent out by firms giving details of the upcoming payment and the various trade elements which are used in the calculation. The payment notice highlights the currency, value date, amount, payer, receiver, account details, rate, period, trade identifiers and trade details used in the calculation. Though payment notices are typically auto-generated from internal proprietary systems, the communication with counterparty is still manual, through telephone, fax and email. The verification of payment notices is also manual and time consuming from the receiver of the payment notices side. Without uniformed payment notice templates available to communicate the details, there becomes a need for multiple interpretations. The variation of the payment notices for each asset class adds more spice to this process.

Life cycle events

A consistent challenge exists to maintain a single copy of an OTC trade across its life cycle. An event in the life cycle involves a cash movement between two counterparties. A correct identification of the life cycle event ensures correct payment calculation. There is no industry data available to identify the number of payments across each payment type. Any change in the benchmark floating rate triggers a control point to validate the payment. Every incoming or outgoing payment due before the value date is sent to the Treasury unit for funding. The Treasury group in an organization ensures that the required amount is made available in the Nostro account for outgoing payments and earns interest on the additional funds by investing in the overnight market.

Payments related to collateral movement and initial margin

Cash is the most preferred asset used as collateral against a counterparty risk. Initial margin is typically referred to as a good faith deposit or a cushion payment to cover initial day market movement and can be requested intra-day on volatile market days. Thresholds and minimum transfer amounts are other parameters used to identify whether payment transactions should be made. Thresholds allow counterparties to take a certain amount of unsecured credit risk equal to the threshold without requiring collateral to cover the additional risk. Though cash by nature is fungible, collateral management process demands that independent amount, initial margin and variation margin can be tracked to each customer. The recent financial crisis, regulations and industry initiatives have ensured that this traceability is necessary for the financial system.

The effect of incorrect payments

The majority of firms have write-off policies to protect against failed transactions, which can occur for multiple reasons. Amounts to be written off can include back valuation charges, interest compensation, loss on funding and overdraft charges. Such charges directly impact the firms’ P&L. Depending on the trade situation an incorrect payment can be back-valued. When payments are back-valued, there are two outcomes:

• The paying party pays the back valuation charges and the receiving party receives the payment on a specified good value date
• The paying party chooses to make a late payment in which the payment is delivered to the receiver with a new value date and the paying party pays the late payment fees

Then, there is also the manual process involved with the reversal of incorrect payments and the cumbersome reconciliation process that must take place to re-receive and or re-send a payment. While incorrect payments vary based on the particular product specification, and in general represent only a small fraction of all payments made, the losses associated with incorrect payments can be substantial. A missed or incorrect payment may lead to complete portfolio reconciliation. There are no industry wide guidelines for OTC products. Thus each institution has set its own policies and guidelines for processing interest compensation claims.

The effect of central clearing

Clearing houses mediate every buy and sell of member firms. The member firm pays membership fees and posts collateral for every trade cleared at a clearinghouse. With the Dodd-Frank Act mandating more centrally cleared trades, there will be increased transparency (mark-to-market pricing) all of which can be monitored. Cash flow and collateral reports for OTC trades from clearing houses are still evolving. Firms using clearing members should demand added transparency on their collateral and payment flows. In addition the clearing members’ demand for fees to clear the trade should be negotiated. The back loading of the trades from the traditional OTC cleared world should include updating of the payment process and instructions.

Reconciliation

Reconciliation of payments for OTC derivatives poses some of the greatest business challenges due to their definite economic impact. Un-reconciled payments directly increase firms’ liability. Firms monitor their Nostro accounts closely to make sure that no unidentified counterparty cash is left sitting in them. The reconciliation process is extremely manual and labor intensive, consisting of excel spreadsheets and novated agreements between counterparties. Reconciliation methodologies are usually proprietary to firms and reconciliation break accounting is manual.

Industry initiatives such as trade netting and portfolio compression are used to reduce the number of trades and payments to be processed. The number of breaks reconciled within two days has increased across asset classes.

Conclusion

Payment settlement matching is one of the least automated functions in the OTC trade lifecycle function. Payment processing for certain trade events should not be automated due to trade complexity, counterparty complexity or the nature of the payment. With the renewed focus on risk management, counterparty exposure calculation is one of the prime functions in credit risk management including payments in transit, failed payments, past due and currently unsettled payments with the counterparty. A non-payment incident immediately triggers the risk management team to re-evaluate the counterparty exposure risk. Settlement functions are performed in a low cost centre. However, increasing value additional services in payment processing for OTC derivatives either through process efficiency or automation still remains the major factor.

Wednesday, March 23, 2011

Abstract Factory

Motivation
Modularization is a big issue in today's programming. Programmers all over the world are trying to avoid the idea of adding code to existing classes in order to make them support encapsulating more general information. Take the case of a information manager which manages phone number. Phone numbers have a particular rule on which they get generated depending on areas and countries. If at some point the application should be changed in order to support adding numbers form a new country, the code of the application would have to be changed and it would become more and more complicated.

In order to prevent it, the Abstract Factory design pattern is used. Using this pattern a framework is defined, which produces objects that follow a general pattern and at runtime this factory is paired with any concrete factory to produce objects that follow the pattern of a certain country. In other words, the Abstract Factory is a super-factory which creates other factories (Factory of factories).

Intent
Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes.

Applicability & Examples

•We should use the Abstract Factory design pattern when:
•The system needs to be independent from the way the products it works with are created.
•The system is or should be configured to work with multiple families of products.
•A family of products is designed to work only all together.
•The creation of a library of products is needed, for which is relevant only the interface, not the implementation, too.


Phone Number Example
The example at the beginning of the article can be extended to addresses, too. The AbstractFactory class will contain methods for creating a new entry in the information manager for a phone number and for an address, methods that produce the abstract products Address and PhoneNumber, which belong to AbstractProduct classes. The AbstractProduct classes will define methods that these products support: for the address get and set methods for the street, city, region and postal code members and for the phone number get and set methods for the number.

The ConcreteFactory and ConcreteProduct classes will implement the interfaces defined above and will appear in our example in the form of the USAddressFactory class and the USAddress and USPhoneNumber classes. For each new country that needs to be added to the application, a new set of concrete-type classes will be added. This way we can have the EnglandAddressFactory and the EnglandAddress and EnglandPhoneNumber that are files for English address information.

Pizza Factory Example
Another example, this time more simple and easier to understand, is the one of a pizza factory, which defines method names and returns types to make different kinds of pizza. The abstract factory can be named AbstractPizzaFactory, RomeConcretePizzaFactory and MilanConcretePizzaFactory being two extensions of the abstract class. The abstract factory will define types of toppings for pizza, like pepperoni, sausage or anchovy, and the concrete factories will implement only a set of the toppings, which are specific for the area and even if one topping is implemented in both concrete factories, the resulting pizzas will be different subclasses, each for the area it was implemented in.

Look & Feel Example
Look & Feel Abstract Factory is the most common example. For example, a GUI framework should support several look and feel themes, such as Motif and Windows look. Each style defines different looks and behaviors for each type of controls: Buttons and Edit Boxes. In order to avoid the hardociding it for each type of control we define an abstract class LookAndFeel. This calls will instantiate, depending on a configuration parameter in the application one of the concrete factories: WindowsLookAndFeel or MotifLookAndFeel. Each request for a new object will be delegated to the instatiated concrete factory which will return the controls with the specific flavor

Hot Points:
·     AbstractFactory class declares only an interface for creating the products. The actual creation is the task of the ConcreteProduct classes, where a good approach is applying the Factory Method design pattern for each product of the family.
  • Extending factories can be done by using one Create method for all products and attaching information about the type of product needed.

Singleton Pattern

Motivation

Sometimes it's important to have only one instance for a class. For instance, there should be only one window manager (or only a file system or only a print spooler) in a system. Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves.
The singleton patterns is one of the simplest desing patterns and involves only one class which is responsible to instantiate itself only one instance and in the same time to provide a global point of access to the instance. In that case the instance can be used from everywhere without calling the directly the constructor each time.

Intent

•Ensure that only one instance of a class is created.
•Provide a global point of access to the object.

Implementation

The implementation involves a static member in the "Singleton" class, a private constructor and a static public method that returns a reference to the static member.

Applicability & Examples

According to the definition the singleton pattern should be used when there must be exactly one instance of a class, and when it must be accessible to clients from a global access point.

Here are some real situations where the singleton is used:


Example 1 - Logger Classes

The Singleton pattern is used in the design of logger classes. This classes are ussualy implemented as a singletons, and provides a global logging access point in all the application components without being necessary to create an object each time a logging operations is performed.

Example 2 - Configuration Classes

The Singleton pattern is used to design the classes that provide the configuration settings for an application. By implementing configuration classes as Singleton not only that we provide a global access point, but we also keep the instance we use as a cache object. When the class is instantiated( or when a value is read ) the singleton will keep the values in its internal structure. If the values are read from the database or from files this avoid reloading the values each time the configuration parameters are used.

Example 3 - Accesing resources in shared mode

It can be used in the design of an application that needs to work with the serial port. Let's say that there are many classs in the application, working in an multithreading environment, that needs to operate actions on the serial port. In this case a singleton with synchronized methods has to be used to manage all the operations on the serial port.

Abstract v/s Interface

What is an Abstract Class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn�t support multiple inheritance, interfaces are used to implement multiple inheritance.

Cursor and alteranative of Cursor

If possible, avoid using SQL Server cursors. They generally use a lot of SQL Server resources and reduce the performance and scalability of your applications. If you need to perform row-by-row operations, try to find another method to perform the task.

Here are some alternatives to using a cursor:

• Use WHILE LOOPS
• Use temp tables
• Use derived tables
• Use correlated sub-queries
• Use the CASE statement
• Perform multiple queries

If you do find you must use a cursor, try to reduce the number of records to process.

One way to do this is to move the records that need to be processed into a temp table first, and then create the cursor to use the records in the temp table, not from the original table. This of course assumes that the subsets of records to be inserted into the temp table are substantially less than those in the original table.
The lower the number of records to process, the faster the cursor will finish.
If you have no choice but to use a server-side cursor in your application, try to use a FORWARD-ONLY or FAST-FORWARD, READ-ONLY cursor. When working with unidirectional, read-only data, use the FAST_FORWARD option instead of the FORWARD_ONLY option, as it has some internal performance optimizations to speed performance. This type of cursor produces the least amount of overhead on SQL Server.
If you are unable to use a fast-forward cursor, then try the following cursors in this order, until you find one that meets your needs. They are listed in the order of their performance characteristics, from fastest to slowest: dynamic, static, and keyset.

Avoid using static/insensitive and keyset cursors, unless you have no other choice. This is because they cause a temporary table to be created in TEMPDB, which increases overhead and can cause resource contention issues.

If you have no choice but to use cursors in your application, try to locate the SQL Server tempdb database on its own physical device for best performance. This is because cursors use the tempdb for temporary storage of cursor data. The faster your disk array running tempdb, the faster your cursor will be.

Using cursors can reduce concurrency and lead to unnecessary locking and blocking. To help avoid this, use the READ_ONLY cursor option if applicable, or if you need to perform updates, try to use the OPTIMISTIC cursor option to reduce locking. Try to avoid the SCROLL_LOCKS cursor option, which reduces concurrency.

When you are done using a cursor, don't just CLOSE it, you must also DEALLOCATE it. Deallocation is required to free up the SQL Server resources used by the cursor. If you only CLOSE the cursor, locks are freed, but SQL Server resources are not. If you don't DEALLOCATE your cursors, the resources used by the cursor will stay allocated, degrading the performance of your server until they are released.

If it is appropriate for your application, try to load the cursor as soon as possible by moving to the last row of the result set. This releases the share locks created when the cursor was built, freeing up SQL Server resources.

If you have to use a cursor because your application needs to manually scroll through records and update them, try to avoid client-side cursors, unless the number of rows is small or the data is static. If the number of rows is large, or the data is not static, consider using a server-side keyset cursor instead of a client-side cursor. Performance is usually boosted because of a reduction in network traffic between the client and the server. For optimum performance, you may have to try both types of cursors under realistic loads to determine which is best for your particular environment.

When using a server-side cursor, always try to fetch as small a result set as possible. This includes fetching only those rows and columns the client needs immediately. The smaller the cursor, no matter what type of server-side cursor it is, the fewer resources it will use, and performance will benefit.

If you need to perform a JOIN as part of your cursor, keyset and static cursors are generally faster than dynamic cursors, and should be used when possible.

STL containers

What are the types of STL containers?

• deque
• hash_map
• hash_multimap
• hash_multiset
• hash_set
• list
• map
• multimap
• multiset
• set
• vector


Currently there are two major types of containers defined in the C++ library: sequence containers and associative containers.
Sequence containers are: vector deque list
Associative containers are: set multiset map and multimap.

There are 3 containers adapters: stack queue and priority_queue.

The STL has two more data structures that some people argue they might be containers as well: bitset and valarray. There is also the specialization of the vector container for booleans which might not be a container.

No hash containers are defined in the current C++ STL standard. However other STL implementation might have some hash based containers (ie STL SGI implementation).

The upcoming C++ standard defines among others in TR1 (Technical Report 1) new containers : array unorder_set unordered_multiset unorder_map and unordered_multimap. Most of the TR1 is implemented in the gcc compiler. Dinkumware has a full TR1 specification implementation.

Wednesday, March 16, 2011

Data Type


"P" means "pointer"
"W" means "wide"
"STR" means "string"
"C" means "const"


PWSTR means "pointer of wide string", that is, unsigned short* (or wchar_t*)

LPSTR means "long pointer of string", that is, char*

WCHAR means "wide char", that is, unsigned short (or wchar_t)

LPCSTR means "const pointer of string", that is, const char*

PWSTR is a pointer to a UNICODE string.
[ typedef WCHAR* PWSTR; typedef wchar_t WCHAR; ]

LPSTR is a pointer to a 8 bit ANSI string
[typedef CHAR* LPSTR; typedef char CHAR; ]

WCHAR is a wchar_t as mentioned above and LPCSTR is an 8 bit ANSI string
[ typedef __nullterminated CONST CHAR* LPCSTR; ]

These typedefs can be found in file WinNT.h



Char/wchar/TCHAR : The C strings for ANSI and Unicode

CString : The C++/MFC class wrapper for C strings

BSTR : The Visual Basic string type

_bstr_t : A C++ class wrapper for the Visual Basic string type

CComBSTR : Yet another C++ class wrapper for the Visual Basic

string type used predominately in ATL code

Thursday, March 3, 2011

Most popular Used Regular Expression

Internet Email: \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Internet URL: http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
US Phone Number: ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
US Social Security Number: \d{3}-\d{2}-\d{4}
US ZIP Code: \d{5}(-\d{4})?
URI: \b(\S+)://([^:]+)(?::(\S+))?\b

Tuesday, March 1, 2011

List of Querying the SQL Server System Catalog

How do I find the data types of the columns of a specified table?
Before you run the following query, replace and with valid names.

USE ;
GO
SELECT c.name AS column_name
,c.column_id
,SCHEMA_NAME(t.schema_id) AS type_schema
,t.name AS type_name
,t.is_user_defined
,t.is_assembly_type
,c.max_length
,c.precision
,c.scale
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
WHERE c.object_id = OBJECT_ID('')
ORDER BY c.column_id;
GO

How do I find all the owners of entities contained in a specified schema?

USE ;
GO
SELECT 'OBJECT' AS entity_type
,USER_NAME(OBJECTPROPERTY(object_id, 'OwnerId')) AS owner_name
,name
FROM sys.objects WHERE SCHEMA_NAME(schema_id) = ''
UNION
SELECT 'TYPE' AS entity_type
,USER_NAME(TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'OwnerId')) AS owner_name
,name
FROM sys.types WHERE SCHEMA_NAME(schema_id) = ''
UNION
SELECT 'XML SCHEMA COLLECTION' AS entity_type
,COALESCE(USER_NAME(xsc.principal_id),USER_NAME(s.principal_id)) AS owner_name
,xsc.name
FROM sys.xml_schema_collections AS xsc JOIN sys.schemas AS s
ON s.schema_id = xsc.schema_id
WHERE s.name = '';
GO

How do I find all the tables that do not have a primary key?

USE ;
GO
SELECT SCHEMA_NAME(t.schema_id) AS schema_name
,t.name AS table_name
FROM sys.tables t
WHERE object_id NOT IN
(
SELECT parent_object_id
FROM sys.key_constraints
WHERE type_desc = 'PRIMARY_KEY_CONSTRAINT' -- or type = 'PK'
);
GO

Or, you can run the following query.

USE ;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
,name AS table_name
FROM sys.tables
WHERE OBJECTPROPERTY(object_id,'TableHasPrimaryKey') = 0
ORDER BY schema_name, table_name;
GO

How do I find all the tables that do not have an index?

USE ;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
,name AS table_name
FROM sys.tables
WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0
ORDER BY schema_name, table_name;
GO

How do I find all the tables that have an identity column?

USE ;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
, t.name AS table_name
, c.name AS column_name
FROM sys.tables AS t
JOIN sys.identity_columns c ON t.object_id = c.object_id
ORDER BY schema_name, table_name;
GO

Or, you can run the following query.
USE ;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
,name AS table_name
FROM sys.tables
WHERE OBJECTPROPERTY(object_id,'TableHasIdentity') = 1
ORDER BY schema_name, table_name;
GO

How do I find all the stored procedures in a database?

USE ;
GO
SELECT name AS procedure_name
,SCHEMA_NAME(schema_id) AS schema_name
,type_desc
,create_date
,modify_date
FROM sys.procedures;
GO

How do I find the parameters for a specified stored procedure or function?

USE ;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
,o.name AS object_name
,o.type_desc
,p.parameter_id
,p.name AS parameter_name
,TYPE_NAME(p.user_type_id) AS parameter_type
,p.max_length
,p.precision
,p.scale
,p.is_output
FROM sys.objects AS o
INNER JOIN sys.parameters AS p ON o.object_id = p.object_id
WHERE o.object_id = OBJECT_ID('')
ORDER BY schema_name, object_name, p.parameter_id;
GO

How do I find all the user-defined functions in a database?

USE ;
GO
SELECT name AS function_name
,SCHEMA_NAME(schema_id) AS schema_name
,type_desc
,create_date
,modify_date
FROM sys.objects
WHERE type_desc LIKE '%FUNCTION%';
GO

How do I find all views in a database?

USE ;
GO
SELECT name AS view_name
,SCHEMA_NAME(schema_id) AS schema_name
,OBJECTPROPERTYEX(object_id,'IsIndexed') AS IsIndexed
,OBJECTPROPERTYEX(object_id,'IsIndexable') AS IsIndexable
,create_date
,modify_date
FROM sys.views;
GO

How do I find all the entities that have been modified in the last N days?

USE ;
GO
SELECT name AS object_name
,SCHEMA_NAME(schema_id) AS schema_name
,type_desc
,create_date
,modify_date
FROM sys.objects
WHERE modify_date > GETDATE() -
ORDER BY modify_date;
GO

How do I view the definition of a server-level trigger?

SELECT definition
FROM sys.server_sql_modules;
GO

How do I find the columns of a primary key for a specified table?

USE ;
GO
SELECT i.name AS index_name
,ic.index_column_id
,key_ordinal
,c.name AS column_name
,TYPE_NAME(c.user_type_id)AS column_type
,is_identity
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns AS c
ON ic.object_id = c.object_id AND c.column_id = ic.column_id
WHERE i.is_primary_key = 1
AND i.object_id = OBJECT_ID('');
GO

Or, you can use the COL_NAME function as shown in the following example.

USE ;
GO
SELECT i.name AS index_name
,COL_NAME(ic.object_id,ic.column_id) AS column_name
,ic.index_column_id
,key_ordinal
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1
AND i.object_id = OBJECT_ID('');
GO

How do I find the columns of a foreign key for a specified table?

USE ;
GO
SELECT
f.name AS foreign_key_name
,OBJECT_NAME(f.parent_object_id) AS table_name
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name
,OBJECT_NAME (f.referenced_object_id) AS referenced_object
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name
,is_disabled
,delete_referential_action_desc
,update_referential_action_desc
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.object_id = fc.constraint_object_id
WHERE f.parent_object_id = OBJECT_ID('');

How do I determine if a column is used in a computed column expression?

USE ;
GO
SELECT OBJECT_NAME(object_id) AS object_name
,COL_NAME(object_id, column_id) AS computed_column
,class_desc
,is_selected
,is_updated
,is_select_all
FROM sys.sql_dependencies
WHERE referenced_major_id = OBJECT_ID('')
AND referenced_minor_id = COLUMNPROPERTY(referenced_major_id, '', 'ColumnId')
AND class = 1;
GO

How do I find all the columns that are used in a computed column expression?

USE ;
GO
SELECT OBJECT_NAME(d.referenced_major_id) AS object_name
,COL_NAME(d.referenced_major_id, d.referenced_minor_id) AS column_name
,OBJECT_NAME(referenced_major_id) AS dependent_object_name
,COL_NAME(d.object_id, d.column_id) AS dependent_computed_column
,cc.definition AS computed_column_definition
FROM sys.sql_dependencies AS d
JOIN sys.computed_columns AS cc
ON cc.object_id = d.object_id AND cc.column_id = d.column_id AND d.object_id=d.referenced_major_id
WHERE d.class = 1
ORDER BY object_name, column_name;
GO

How do I find all the constraints for a specified table?

USE ;
GO
SELECT OBJECT_NAME(object_id) as constraint_name
,SCHEMA_NAME(schema_id) AS schema_name
,OBJECT_NAME(parent_object_id) AS table_name
,type_desc
,create_date
,modify_date
,is_ms_shipped
,is_published
,is_schema_published
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
AND parent_object_id = OBJECT_ID('');
GO

How do I find all the indexes for a specified table?

USE ;
GO
SELECT i.name AS index_name
,i.type_desc
,is_unique
,ds.type_desc AS filegroup_or_partition_scheme
,ds.name AS filegroup_or_partition_scheme_name
,ignore_dup_key
,is_primary_key
,is_unique_constraint
,fill_factor
,is_padded
,is_disabled
,allow_row_locks
,allow_page_locks
FROM sys.indexes AS i
INNER JOIN sys.data_spaces AS ds ON i.data_space_id = ds.data_space_id
WHERE is_hypothetical = 0 AND i.index_id <> 0
AND i.object_id = OBJECT_ID('');
GO

Friday, February 25, 2011

Information related to the security admin server role and system admin server role

Security is a very important aspect that a DBA should know about. It is also important to know which login has a sysadmin or security admin server level role. The following SQL Command will show information related to the security admin server role and system admin server role.

SELECT l.name, l.denylogin, l.isntname, l.isntgroup, l.isntuser
FROM master.dbo.syslogins l
WHERE l.sysadmin = 1 OR l.securityadmin = 1

Server level Configuration Information

Server level configuration controls some of the features and performance of SQL Server. It is also important for a SQL Server DBA to know the server level configuration information. The following SQL Statement will give all of the information related to Server level configuration.

SELECT * from sys.configurations order by NAMEIf you are using SQL Server 2000, you can execute the following command instead.

SP_CONFIGURE 'show advanced options',1
go
RECONFIGURE with OVERRIDE
go
SP_CONFIGURE
go

SERVERPROPERTY

The following T-SQL statement retrieves information such as Hostname, Current instance name, Edition, Server type, ServicePack and version number from current SQL Server connection. 'Edition' will give information on a 32 bit or 64 bit architecture and 'Productlevel' gives information about what service pack your SQL Server is on. It also displays if the current SQL Server is a clustered server.

SELECT
SERVERPROPERTY('MachineName') as Host,
SERVERPROPERTY('InstanceName') as Instance,
SERVERPROPERTY('Edition') as Edition, /*shows 32 bit or 64 bit*/
SERVERPROPERTY('ProductLevel') as ProductLevel, /* RTM or SP1 etc*/
Case SERVERPROPERTY('IsClustered') when 1 then 'CLUSTERED' else
'STANDALONE' end as ServerType,
@@VERSION as VersionNumber

Thursday, February 24, 2011

Kill ALL Connections To a SQL Database

The Kill Connection script utilizes sys.dm_tran_locks (syslockinfo for SQL 2000) to capture SPIDs that are accessing the specified database. The traditional way of using sysprocesses table alone can not detect distributed transactions or cross-database references to the database.


Create Proc [dbo].[usp_killConnections]
@db_name Nvarchar(200)
AS
set nocount on
if db_id(@db_name) is null
return

declare @spid int
declare spid cursor for

/* For SQL 2000 and SQL 2005/2008 Backward compatible mode*/

select spid from master.dbo.sysprocesses(nolock) where dbid = db_id(@db_name) and spid > 50
union
select distinct req_spid from sys.syslockinfo(nolock) where rsc_dbid = db_id(@db_name) and req_spid > 50

/* For SQL 2005/2008 */
/*
select spid from master.dbo.sysprocesses(nolock) where dbid = db_id(@db_name) and spid > 50
union
select distinct request_session_id from sys.dm_tran_locks (nolock) where resource_database_id = db_id(@db_name) and request_session_id > 50
*/

open spid
fetch next from spid
into @spid

while @@fetch_status = 0
begin
exec ('kill ' + @spid)
fetch next from spid into @spid
end

close spid
deallocate spid

String tokenizing / splitting

Simply copy and paste the Split() function code in your T-SQL code editor, to select appropriate DB from database combo box and then press F5 key to create user defined Split() function.

Now you can use it as follows:
1. The first parameter of function is string that you need to split.
2. And second parameter of function is delimiter.

SELECT * FROM dbo.Split ('Token1;Token2;Token3;Token4;Token5',';')

The Expected output will be as follow:
Token1
Token2
Token3
Token4
Token5



CREATE FUNCTION dbo.Split
(
@String VARCHAR(8000),
@Delimiter NVARCHAR(1)
)
RETURNS @Tokens table (Token NVARCHAR(255))
AS
BEGIN
WHILE (CHARINDEX(@Delimiter,@String)>0)
BEGIN
INSERT INTO @Tokens (Token) VALUES
(LTRIM(RTRIM(SUBSTRING(@String,1,CHARINDEX(@Delimiter,@String)-1))))
SET @String = SUBSTRING(@String, CHARINDEX(@Delimiter,@String)+LEN(@Delimiter),LEN(@String))
END
INSERT INTO @Tokens (Token) VALUES (LTRIM(RTRIM(@String)))
RETURN
END

Unused indexes in your databases

This Script allows you to determine the list of unused indexes in your databases


select object_name (i.object_id) as NomTable,isnull( i.name,'HEAP') as IndexName
from sys.objects o inner join sys.indexes i
ON i.[object_id] = o.[object_id] left join
sys.dm_db_index_usage_stats s
on i.index_id = s.index_id and s.object_id = i.object_id
where object_name (o.object_id) is not null
and object_name (s.object_id) is null
AND o.[type] = 'U'
and isnull( i.name,'HEAP') <>'HEAP'

union all

select object_name (i.object_id) as NomTable,isnull( i.name,'HEAP') as IndexName
from sys.objects o inner join sys.indexes i
ON i.[object_id] = o.[object_id] left join
sys.dm_db_index_usage_stats s
on i.index_id = s.index_id and s.object_id = i.object_id
where user_seeks= 0 and user_scans=0 and user_lookups= 0 AND o.[type] = 'U'
and isnull( i.name,'HEAP') <>'HEAP'
order by NomTable asc

order by OBJECT_NAME(i.object_id) ASC

select type,* from sys.objects
where name = 'DDAOeuvre'
where user_seeks=0 and user_scans=0 and user_lookups = 0
order by object_name (s.object_id) asc

ROW COUNT

SELECT o.name AS "Table Name", i.rowcnt AS "Row Count"
FROM sysobjects o, sysindexes i
WHERE i.id = o.id AND indid IN(0,1)
AND xtype = 'u'
AND o.name <> 'sysdiagrams'
ORDER BY i.rowcnt DESC

Searching through SPs, Tables, Views

This little procedure is very useful during development / support to quickly find all objects that may have something to do with a certain term, column name, part of thereof and so on.

CREATE PROCEDURE adhoc_SearchText(@text VARCHAR(1024))
AS
BEGIN
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES T
WHERE charindex(@text, T.TABLE_NAME)>0

SELECT C.TABLE_NAME, C.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE charindex(@text, C.COLUMN_NAME)>0

SELECT V.TABLE_NAME AS VIEW_NAME
FROM information_schema.VIEWS V
WHERE charindex(@text, V.VIEW_DEFINITION)>0

SELECT R.ROUTINE_NAME
FROM information_schema.routines r
WHERE charindex(@text, r.ROUTINE_DEFINITION)>0
END

Wednesday, February 23, 2011

Get the Table definition from Database

Table definition from SQL server tables

sp_help --(followed by a table name)

ex: sp_help EMP

Table definition from Oracle tables

desc --(followed by a table name)

ex: desc EMP

Thursday, February 17, 2011

About your (electronic) self UNIX COMMAND

About your (electronic) self

whoami --- returns your username. Sounds useless, but isn't. You may need to find out who it is who forgot to log out somewhere, and make sure *you* have logged out.
finger & .plan files
of course you can finger yourself, too. That can be useful e.g. as a quick check whether you got new mail. Try to create a useful .plan file soon. Look at other people's .plan files for ideas. The file needs to be readable for everyone in order to be visible through 'finger'. Do 'chmod a+r .plan' if necessary. You should realize that this information is accessible from anywhere in the world, not just to other people on turing.
passwd --- lets you change your password, which you should do regularly (at least once a year). See the LRB guide and/or look at help password.
ps -u yourusername --- lists your processes. Contains lots of information about them, including the process ID, which you need if you have to kill a process. Normally, when you have been kicked out of a dialin session or have otherwise managed to get yourself disconnected abruptly, this list will contain the processes you need to kill. Those may include the shell (tcsh or whatever you're using), and anything you were running, for example emacs or elm. Be careful not to kill your current shell - the one with the number closer to the one of the ps command you're currently running. But if it happens, don't panic. Just try again :) If you're using an X-display you may have to kill some X processes before you can start them again. These will show only when you use ps -efl, because they're root processes.
kill PID --- kills (ends) the processes with the ID you gave. This works only for your own processes, of course. Get the ID by using ps. If the process doesn't 'die' properly, use the option -9. But attempt without that option first, because it doesn't give the process a chance to finish possibly important business before dying. You may need to kill processes for example if your modem connection was interrupted and you didn't get logged out properly, which sometimes happens.
quota -v --- show what your disk quota is (i.e. how much space you have to store files), how much you're actually using, and in case you've exceeded your quota (which you'll be given an automatic warning about by the system) how much time you have left to sort them out (by deleting or gzipping some, or moving them to your own computer).
du filename --- shows the disk usage of the files and directories in filename (without argument the current directory is used). du -s gives only a total.
last yourusername --- lists your last logins. Can be a useful memory aid for when you were where, how long you've been working for, and keeping track of your phonebill if you're making a non-local phonecall for dialling in.