1

I do not plan on changing the size of my dynamic array. The reason I want to create a dynamic array of static arrays (which contain shorts) is so that I can return that array in a function (not defining the size of that array until I'm in that function).

My first question then, is how do I return such an array? Functions don't let you return a pointer without a type & I'm unable to find out how to define a pointer with type array.

This brings me to my second question, which is how do I correctly define a dynamic array of static arrays? I have searched for this online, but none of the answers have been too helpful.

One way to do it is to declare: short (*array)[size] but the problem with this is that I don't know how to initialize the array in that case, and size has to be a literal.

I could do this:

typedef short column[size];
column * row = NULL;
row = malloc(rowMax * sizeof(column));
row[0][0] = 10;

but again, size has to be a literal; and even if size is a literal, I receive an error stating that a value of type "void *" cannot be assigned to an entity of type "column *".

If any of you have a solution that doesn't use vectors that would be greatly appreciated; as the vector class is larger than the array class.

8
  • "as the vector class is larger than the array class." There's no array class in your sample. Commented Mar 3, 2016 at 23:54
  • 1
    And if you are using C++11 or later, you can use std::array for the static array. Commented Mar 3, 2016 at 23:54
  • @RemyLebeau I tried using: short (*array)[size] = new std::array[row]; but that results in the error: argument list for class template std::array is missing. Commented Mar 3, 2016 at 23:58
  • In C++11, size can also be a constexpr value. Commented Mar 4, 2016 at 0:01
  • What is your version of C++? Please show enough code to generate your errors. Please edit your updates into your question. Commented Mar 4, 2016 at 0:03

2 Answers 2

2

In C++11 one could use std::array from <array> and do:

constexpr std::size_t const cols = 42u; // static size
using MyStaticArray = std::array<short, cols>; // static array type
// Or in C++03 and earlier: typedef short MyStaticArray[42u];

std::size_t const rows = someValue; // dynamic size
MyStaticArray * dynamicArray = new MyStaticArray[rows];
// Or better: std::vector<MyStaticArray> dynamicArray(rows);

for (std::size_t row = 0u; row < rows; ++row)
  for (std::size_t col = 0u; col < cols; ++col)
    dynamicArray[row][col] = rows * cols;

PS: Also give upvotes to Remy Lebeau below for his comments helped to improve this post.

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

1 Comment

If you are going to go to the trouble of using std::array, you may as well use std::vector for the dynamic array: std::vector<MyStaticArray> dynamicArray(rows); Even if you are not using C++11, you can still use std::vector, just typedef the static array using typedef short MyStaticArray[cols];
1

For the sake of saving time, just create a vector of static arrays

std::vector<std::array<Type, n>>

Not sure why the size difference matters, it's only a few Kb.

Also, if you're going to write C code in a C++ app... you're gunna have a bad time.

Comments

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.