0

I've typically shied away from unnecessary C++ features but as time marches on I can't avoid confronting my gremlins. Most recent of all is operator new[] and the potential for memory problems.

char *playerNewNames = new char[numPlayers][50];

It's great to know we don't need so many pointer * indirections and I'd feel confident in C iterating with malloc but the above seems a step too far. My compiler didn't complain but I want to be sure I will be getting an array of size numPlayers with each indexing a tranche of 50 characters. And how would I deallocate this?

I will try delete[][] but even if that doesn't throw it isn't 100% obvious that it will clean up everything, without iteration on my part. Please can somebody explain. Thanks in advance.

John
  • 6,433
  • 7
  • 47
  • 82
  • 2
    Don't use `new`. Use `std::array` for constant-size arrays and `std::vector` for dynamic ones. It's that simple. – stefan Aug 13 '14 at 09:24
  • I know char arrays are frowned on but I depend on a lot of legacy code that makes life much easier for me to continue to use. – John Aug 13 '14 at 09:25
  • possible duplicate of [How do I declare a 2d array in C++ using new?](http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) Note: For your case, the comment to the question applies. – Xarn Aug 13 '14 at 09:25
  • There are no `operator new[][]` and `operator delete[][]`. There are `operator new[]` and `operator delete[]` but you should not be using them. Use `std::vector`. – n. m. could be an AI Aug 13 '14 at 09:25
  • Staying away from `new` is a great idea , keep doing it! Use containers which manage memory for you. – M.M Aug 13 '14 at 09:26
  • okay I do it iteratively as Xarn suggested. Thanks all – John Aug 13 '14 at 09:26

3 Answers3

4

char *playerNewNames = new char[numPlayers][50]; is a mistake. You should get a compiler error. The correct syntax is:

char (*playerNewNames)[50] = new char[numPlayers][50];

and to delete it:

delete[] playerNewNames;

However a much better option would be to not use C-style arrays, and not use new. Instead, use container classes which manage the memory for you, such as std::vector or std::array.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • You are right about a compiler error, I hadn't synchronized my disk properly. `char (*playerNewNames)[50]` thanks, that was the code I was lacking :) – John Aug 13 '14 at 09:29
0

The only proper way to deallocate an array is to use delete[]

delete deallocated single object while delete[] dealocates an array of objects. there is no such thing as delete[][] or delete[][][]

codekiddy
  • 5,897
  • 9
  • 50
  • 80
0

It looks like you want an array of arrays.

The most readable way, in my opinion, is to use a type alias.

typedef char PlayerName[50];

PlayerName* playerNewNames = new PlayerName[numPlayers];

delete [] playerNewNames;

If you want an array of pointers to arrays, you're going to need to iterate, but since you want all the "inner" arrays to have the same size, and it is known at compile time, that seems like a bad idea.

And learn about std::vector and std::array.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82