0

I know there is a similar question for this but it doesn't include classes and I couldn't make it work.

class temp
{
    int r, c;
    int** array;
public:
    temp() 
    { 
      r = 0; 
      c = 0; 
      (*array)[c] = new int[r][c]; 
    }
};

I tried this but I keep getting an error.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • And what error is that? – ocrdu Dec 30 '20 at 20:05
  • What do you think `*array` will be when `array` hasn't been initialized? – Adrian Mole Dec 30 '20 at 20:07
  • 2
    1) Arrays are not pointers. 2) Do you want a multidimensional rectangular array, or do you want an array of pointers to arrays (a "ragged" structure)? 3) You can't do the former with both dimensions dynamic. 4) You can't do the latter in one call to `new`. 5) I would greatly suggest finding another way to do this without `new`. `std::vector` or `std::unique_ptr` are ideas. (In those cases 2) still needs to be answered). – HTNW Dec 30 '20 at 20:08
  • Classes are completely irrelevant to the issue of properly allocating a multi-dimensional array. Whether the pointer is a class member, or not, is irrelevant, it's allocated exactly the same way. See the linked question for more information. – Sam Varshavchik Dec 30 '20 at 20:13
  • And I guess the most important point: whether or not it's in a class doesn't actually make a difference; it's mostly the same either way. Go back to the questions you were reading and try to understand them completely first. Also 6) fairly sure `new`ing an array of zero length is UB. – HTNW Dec 30 '20 at 20:14
  • The example you're giving has r and c hardcoded to 0. Is there a reason you're not using `vector`? Is this a homework assignment and you have to use `new`? – Mustafa Ozturk Dec 31 '20 at 00:54

0 Answers0