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