-1

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.

Nilesh Kumar
  • 343
  • 1
  • 5
  • 18
  • 2
    Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here. It's especially important to post a [mcve]. – R Sahu Mar 02 '18 at 04:47
  • You almost certainly have more than one `A` instance, one with a valid pointer, and another without. – Ben Voigt Mar 02 '18 at 04:54
  • 1
    `d2` is uninitialized. – melpomene Mar 02 '18 at 04:56
  • Why are you doing this? What's wrong with `int arr[5][5];`? – user207421 Mar 02 '18 at 05:15
  • you've reinitialized "d1" to 5 ...did you mean "d2" for the second value? – poorgoat Mar 02 '18 at 05:54

2 Answers2

-1

There were a couple of minor issues with your code but overall it was ok:

 1  #include <cstdio>
 2  #include <cstdlib>
 3  #include <iostream>
 4
 5  using namespace std;
 6
 7  class A
 8  {
 9  private:
10      int*    a;
11      int     d1, d2;
12
13  public:
14      A()
15      {
16          a           = new int[5 * 5];
17          cout << (void*)a << endl; // this prints a address
18          d1    = 5;
19          d2    = 5;
20      }
21
22      void    chk()
23      {
24          if( a == nullptr )
25          {
26              cout << "a has gone wild\n";
27          } // this if condition is true always
28          else
29          {
30              cout << "a was ok\n";
31              for( int i = 0; i < d1; i++ )
32              {
33                  for( int j = 0; j < d2; j++ )
34                  {
35                      a[i * d2 + j] = 10; // some random value
36                  }
37              }
38          }
39      }
40  };
41
42  int    main( void )
43  {
44      A a;
45      a.chk();
46
47      return 0;
48  }

And the output when running it:

<44680> ./test
0x16d7c20
a was ok
edwinc
  • 1,658
  • 1
  • 14
  • 14
  • Please don't paste line numbers. It makes it horrific to verify the snippet – Passer By Mar 02 '18 at 07:46
  • But for code discussions line numbers help. On any IDE, one can choose the first two columns and remove them. And on vi/emacs one can remove the first few chars on every line easily. I spent quite some time writing a proper answer and not sure if the overall negative points i earned were just due to the line numbers in the solution. – edwinc Sep 16 '20 at 20:33
  • SO isn't for discussions, it's for Q&A. If you're posting anything that needs line numbers, you're posting something too long. – Passer By Sep 17 '20 at 02:21
-2

First of all think about how do you make your 2D array: if you want to make static 2D array, just use,

int arr[5][5];

If you want to make dynamic 2D array,

int** arr=new int*[5];
for(int k=0; k<i; k++)
arr[k]=new int [5];

Also check up what are you doing here this->d1=5 this->d1=5

Yauhen Mardan
  • 327
  • 3
  • 10