I have a class called MyClass , with a print given when the constructor or distractor is called. I am trying to allocate memory from new operator. I have some question on the output of below code.
<code>
#include...
class MyClass
{
public:
MyClass()
{
cout << "MyClass Object Created \n";
}
~MyClass()
{
cout << "MyClass Object Deleted\n+";
}
};
int main(int argc, const char * argv[])
{
int size = 0; // if the size is zero
std::cout << "Hello, World!\n";
MyClass *mclass = NULL;
cout << "before allocating the memory :" << mclass << endl;
mclass = new MyClass[size](); //object will not be constructed.
cout << "after allocating the memory :"<< mclass << endl;
delete [] mclass;
cout << "after deallocating the memory :"<< mclass << endl;
return 0;
}
</code>
Questions - 1. if array size is 1, 2,... any int value constructor is called but when the array size is 0 constructor is not called though memory is allocated
- according to new operator if the memory is allocated constructor should be called.