I've declared a pointer to hold a dynamic 2D array, and allocated memory to it using 'new' in class constructor but it is always equal to nullptr when checked using if statement. The code goes like this:
class A
{
private:
int* a;
int d1, d2;
public:
A()
{
a = new int [5 * 5];
cout << a; //this prints a address
this->d1 = 5;
this->d1 = 5;
}
void chk()
{
if(a == nullptr)
{cerr << "a has gone wild";} // this if condition is true always
else
{
for(int i = 0; i < d1; i++)
{
for(int j = 0; j < d2; j++)
{
a[i * d2 + j] = 10; //some random value
}
}
}
}
};
when I do this same thing i.e. assigning a value to pointer using new in main() without using classes it works fine.
Please suggest what am I missing, where I am going wrong.