2 Dimensional Arrays

Just have a question, why do I need to specify the size of a 2 dimensional array. Is there a way around it, I've tried using a pointer but still gives me an error.

1
2
  int *numbers;
	numbers = new int[4][4];
Last edited on
For a 1d array yo have to specify the size don't you? You can't just have int arr[];
Otherwise, the compiler couldn't generate the code necessary to move the stack pointer down the required distance to create space for your array.

You can do: int arr[] = {2, 4, 5}; for example, because the compiler can figure out from this, that you need 3 * sizeof(int).

In the same way you can do:
int arr[][2] = {{2, 3}, {4,4}};
because once again the compiler can figure out the size of memory needed from that.

In general, for multi-dimensional arrays, if you're going to be initialising it directly as shown above, you can leave out the first dimension.

The same logic applies on the heap, you can't just do: int *arr = new int[];, because how does the system know how much space to allocate? In this case though, you can't do the same sort of direct initialisation, so you have to supply all the dimensions to get the right amount of space allocated.

EDIT:
It's occurred to me that your question may be how to create a multi-dimensional array on the heap.
In the case of a 2d array of int, which is simply an array of arrays, your basic type would be pointer to array of int, so:
int (* numbers)[4] = new int[4][4];

You still need both dimensions, and the second dimension must be a constexpr
Last edited on
Hey Thanks for your reply mate.
in 1 dimensional arrays I tried using a counter variable to specify the size, is that legal.

1
2
3
4
5
6
7
int counter = 0;
float *grades = new float[];
while (true)
{
    grades[counter] = student1->GetFinalNumericGrade(); // returns float
    counter++;
}


Didn't include all the code, but it works just wondering can it be done like that or is this bad coding?
Last edited on
float* grades = new float[];

This doesn't work. You need a size for the allocated array.

float* grades = new float[x];

If you want a multidimensional array:

float** grades = new float[x][y];

Note the pointer to a pointer to a float syntax.

@sourcedesigns I see what you're trying to do up there, but that's not how this works. You expect the array to grow dynamically because you're incrementing counter, but it won't. You will hit an access violation error immediately.
Once your array is defined, that's it. The memory has been allocated for it and you're stuck with that memory. Should you need a bigger array, you have to allocate a new array of required size and move the elements of your old array to the new one.
This sort of thing is error prone and laborious, which is why the standard library has many containers that do just this, i.e. grow automatically.
For example, vector:
1
2
3
4
5
6
7
8
9
10
std::vector<double> grades;
while (true)
{
    grades.push_back(*(student1->GetFinalNumericGrade()));
}

for (size_t i = 0; i < grades.size(); ++i) // there are better ways, but I want to call your attention to size(), in case you're missing "counter"
{
    std::cout << grades[i] << std::endl;
}
Thanks very much tipaye. Helped a lot.
Topic archived. No new replies allowed.