Eskalior
Eskalior

Reputation: 252

How to get the length of multidimensional struct arrays?

I've got a class that gets a 3-dimensional struct array in the constructor. For further calculations it also needs the length of each dimension.

a short example:

MyStruct*** mySt;
mySt = new MyStruct**[5]
mySt[0] = new MyStruct*[4]
mySt[0][0] = new MyStruct[3]

How can I get those values (5, 4, 3) back to save them in the new class without explicitely sending them to the constructor?

Upvotes: 3

Views: 84

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

You cannot get sizes from plain C++ arrays allocated with new and stored as pointers: there is no language construct that would let you retrieve an array size.

There are two options for how to deal with this situation:

  • In C++ use a container that supports sizing - for example, std::vector of vectors of vectors, or
  • In C (or in C++, if you prefer to stay with arrays) construct a separate 3D array of sizes, and pass it along with the mySt array.

Upvotes: 3

Related Questions