How do I get the length of multidimensional structured arrays?

I have a class that receives a 3D array of a struct in a constructor. Further calculations also require the length of each measurement.

short example:

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

      

How can I return these values ​​(5, 4, 3) so that I can store them in a new class without explicitly sending them to the constructor?

+3


source to share


1 answer


You cannot get sizes from simple C ++ arrays allocated with new

and stored as pointers: there is no language construct that allows you to get the size of an array.

There are two options for solving this problem:



  • In C ++, use a container that supports size - like std::vector

    vectors vectors, or
  • In C (or C ++ if you prefer to stay with arrays), create a separate 3-D array of dimensions and pass it along with the array mySt

    .
+3


source







All Articles