Accessing empty vector will always throw exception?

23 07 2008


Vectors are cool! if we access out of array, they will throw unhandled exception. I used to get a lot. 🙂

But are you sure that you’re vector always throw exception if you access out of array?


Answer is NO. Well have a look at the code snippet. At first we access an empty vector which throws an exception. Then we insert some values and then clear the vector to make it empty. Then if we access the empty vector, it won’t throw exception! Have a look at it.

// This class is just to access the protected members
// of vector.
class IntVector : vector<int>
{
    friend void CheckVector();
};

void CheckVector()
{
    IntVector IntArray;

    try
    {
        // Try to access element which result in exception.
        int Value = IntArray[ 0 ];
    }
    catch( ... )
    {
        // It will reach here since we're trying
        // to access an empty vector.
    }

    // Now add one value and clear the vector.
    IntArray.push_back( 10 );
    IntArray.clear();

    try
    {
        // Now try to access element. You can access it
        // eventhough the vector is empty.
        int Value = IntArray[ 0 ];
    }
    catch( ... )
    {
        // It will not reach here.
    }

    // Check the size of memory allocation inside vector.
    int InternalSize = _msize( IntArray._First );
    int VectorSize = IntArray.size();

Well, the reason is optimization. While clearing the vector, for optimization it won’t removes the allocated memory. It just sets the size as 0. So if you access the data by using array operator, you’ll get old value.


The morel is always check the size of array before accessing it. Well the behavior is observed in Visual Studio 6.0. Different IDEs and platforms may show different behavior. Take care!


Targeted Audience – Intermediate.

Advertisements




dynamic_cast throws unhandled exception during Reverse Polymorphism?

30 06 2008

What is Reverse Polymorphism?
Casting Base class object to derived class pointer is called Reverse Polymorphism or Inverse Polymorphism. Check the following code block.

// Base class.
class Base
{
public:
    virtual ~Base() {}
};

// Derived class.
class Derived : public Base
{
public:
    virtual ~Derived() {}
};

void CDialogDlg::OnButton4()
{
    // Base instance.
    Base* pBase = new Base;

    // Reverse Polymorphism. Cast Base Object to derived.
    Derived* pDerived = dynamic_cast<Derived*>( pBase );
}

While casting by using dynamic_cast, possible you may hit un-handled exception as follows.

Ideally dynamic_cast should return null, since its an invalid casting. But why its throwing un-handled exception? Is there something to do?


You’ve to enable RTTI. If RTTI is not enabled, it will throw un-handled exception for reverse polymorphism. For enabling RTTI,

1) Take project settings,
2) Take C/C++ tab.
3) In “C++ Language” category, enable RTTI. See the screenshot.


This is not a standard C++ behavior. This one is checked only in Visual C++ 6.0 compiler. You may or may not encounter the behavior in other Microsoft compiler version or in other compilers. So take care!


Targeted Audience – Intermediate.





Use throw; carefully. Or else, Unhandled exception will be the result.

20 04 2008


Usually throw; is used to rethrow the exception object. But you should be careful. If you call throw; where there is no exception object in the context, then it will result an un-handled exception and abnormal program termination. The interesting part is that, even catch(…) cannot catch such exceptions.


See the sample code snippet below.

try
{
    throw;
}
catch( ... )
{
}

So take care next time.


Targeted Audience – Intermedite.