1

Consider the following case:

int **my_array = new int*[10];
  1. What do we assign to my_array here?
  2. my_array is a pointer that points to what?
  3. Is there any way to iterate through my_array (the pointer) and set up a two-dimensional array of integers (and not int*)?
crashmstr
  • 28,043
  • 9
  • 61
  • 79
user5241471
  • 105
  • 5

2 Answers2

3

We assign to my_array the address of an array. The array contains pointers which can point to other arrays, but don't yet.

Yes, we can do this:

int **my_array = new int*[10];

for(int i=0; i<10; ++i)
  my_array[i] = new int[13];

my_array[2][11] = 500;
Beta
  • 96,650
  • 16
  • 149
  • 150
  • How to deallocate all the dynamically allocated memory? – user5241471 Sep 16 '15 at 17:07
  • 1
    This code leaks if any but the first of the `new[]` expressions throw an exception (hence downvote until fixed). To fix this, use `std::vector>` instead. –  Sep 16 '15 at 17:07
  • @user5241471: First iterate, calling `delete[] my_array[i];`. Then `delete[] my_array`. – Beta Sep 16 '15 at 17:10
  • In opposite direction, why? – user5241471 Sep 16 '15 at 17:12
  • 2
    @elyse: Point taken, but I don't want to throw exception handling (no pun intended) at a beginner who's still struggling with arrays. – Beta Sep 16 '15 at 17:12
  • @user5241471: Try it with pencil and paper, and you'll see. – Beta Sep 16 '15 at 17:13
  • 1
    When you answer people's questions, you're supposed to *help* them, not just make things worse. – Puppy Sep 16 '15 at 17:26
  • 1
    And this is making things tremendously worse. You're being lazy by not even addressing the problem. Which *you* have to address because you didn't re-use somebody else's solution. This code has crippling safety problems and should never be used by anybody, least of all a beginner who could never hope to address them competently. – Puppy Sep 16 '15 at 17:34
  • @Beta why throw `new[]` at them instead of `std::vector`, then? `new[]` is not for beginners, and almost never used by good experienced C++ programmers. –  Sep 16 '15 at 18:46
  • @elyse: Do I really have to point out that the question was about `new[]`? I know that some people prefer to teach beginners about the standard containers *instead of* dynamic structures; I prefer to teach them about dynamic structures *before* the standard containers (in the same way I'd prefer to teach someone about fire before the internal combustion engine). – Beta Sep 16 '15 at 20:26
  • @Beta: Except the ICE comes pre-packaged so they don't need to do anything to make it work safely, and all they can do with fire is set themselves on fire. – Puppy Sep 16 '15 at 21:28
1

What do we assign to my_array here?

You can assign an int* to the elements of my_array. E.g.

my_array[0] = new int[20];

or

int i;
my_array[0] = &i;

my_array is a pointer that points to what?

It points to an an array of 10 int* objects.

Is there any way to iterate through my_array (the pointer) and set up a two-dimensional array of integers (and not int*)?

Not sure what you are expecting to see here. An element of my_array can only be an int*.

If you want my_array to be a pointer to a 2D array, you may use:

int (*my_array)[20] = new int[10][20];

Now you can use my_array[0][0] through my_array[9][19].

PS If this is your attempt to understand pointers and arrays, it's all good. If you are trying to deploy this code in a working program, don't use raw arrays any more. Use std::vector or std::array.

For a 1D array, use:

// A 1D array with 10 elements.
std::vector<int> arr1(10);

For a 2D array, use:

// A 2D array with 10x20 elements.
std::vector<std::vector<int>> arr2(10, std::vector<int>(20)); 
R Sahu
  • 204,454
  • 14
  • 159
  • 270