1

Please consider this code.

#include <iostream>

using namespace std;

class sample
{
public:
    sample()
    {
        cout << "consructor called" << endl;
    }
    void test()
    {
        cout << "Test function" << endl;
    }
};

int main()
{
    sample *s = nullptr;
    try
    {
        s = new sample[50000000000000000000];
        cout << "allocated" << endl;
    }
    catch(bad_alloc& ba)
    {
        cout << ba.what() << endl;
    }

    s[1].test();

    return 0;
}

It is throwing the bad_alloc exception, but s[1].test(); prints test function on the screen. My doubt is if allocation is not successful, then how object can call the member function.

Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131

0 Answers0