0

I would like to define int array in which first dimension is choosen dynamically but second is static -- something like int array[][8]. Unfortunatelly I can not allocate such array dynamically. int ary[][8] = new int[11][8]; Produces error:

error: initializer fails to determine size of ‘array’
    6 |     int array[][8] = new int[11][8];
      |                      ^~~~~~~~~~~~~~
2d.cpp:6:20: error: array must be initialized with a brace-enclosed initializer

or when I try following code:

int array[][8] = new int*[11];
array[0] = new int[8];

I get

2d2.cpp:6:22: error: initializer fails to determine size of ‘array’
    6 |     int array[][8] = new int*[11];
      |                      ^~~~~~~~~~~~
2d2.cpp:6:22: error: array must be initialized with a brace-enclosed initializer
2d2.cpp:7:25: error: incompatible types in assignment of ‘int*’ to ‘int [8]’
    7 |     array[0] = new int[8];

Is that even possible in c++?

1
  • 1
    std::vector<std::array<int, 8>> myArray(11); Commented Apr 27, 2020 at 15:38

2 Answers 2

1

Just use std::vector and std::array:

#include <vector>
#include <array>
#include <iostream>

int main()
{
    using MyArray = std::vector<std::array<int, 8>>;
    MyArray arr {11};
    for (int i {0}; i < 8; ++i)
        arr[i][i] = i;
    for (const auto& v : arr) {
        for (auto x : v) {
            std::cout << x << " ";
        }
        std::cout << std::endl;
    }
}

Live On Coliru

Sign up to request clarification or add additional context in comments.

3 Comments

This is not what I want. I'm confused about c++ syntax in regard of 2d arrays and I want to clarify it.
@Trismegistos It is how to achieve what you want to achieve. Arrays in C++ are dumb, and only a higher level of abstraction can achieve what you are looking for.
@Trismegistos Why not? It's exactly what you asked for: "int array in which first dimension is choosen dynamically but second is static".
1

I will answer my own question:

int (*array)[8] = new int[11][8];

I forgot how bad C++ rules for creating type definitions -- especially including pointers, arrays and function pointers -- are. I added missing parentheses around array and now it is fine.

2 Comments

So how is this any advantage over the std::vector/std::array version? You have to now concern yourself with deallocating that memory. Modern C++ programs should refrain from using new[].
@PaulMcKenzie There's no need to be aggressive. Yes, if this were a "real problem," I'd suggest using that too, and if you hadn't mentioned it I would have commented that as an alternative. But expressions like new int[11][8] are part of the language, like it or not, and there is value in knowing how to use them. Also, if the only concern is remembering to delete the memory, auto arr = std::make_unique<int[][8]>(11); should work fine. std::vector may be conceptually overkill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.