3

I'm encoding N-Dim image cubes into a different image format. I don't know the dimensions of the image until runtime and the library I'm using to read from the original image needs an N-dim array destination buffer as a parameter.

How can I declare such an array in C++? Thanks :)

Sean Peters
  • 337
  • 1
  • 8
  • 2
    it entirely depends on the library.. have you checked the documentation/API? – Karoly Horvath Jan 15 '13 at 09:57
  • 3
    please tell us exactly what kind of parameter your library accepts. If it's `char*` or `pixeltype***` it's not a C++ problem. – Agent_L Jan 15 '13 at 09:58
  • void * buf void being the type is determined at runtime (the image could be stored as shorts, ints, floats etc.) Just a note: I am able to determine the type at runtime. And was planning on just using a switch statement to declare my buffer for each different type. I could do the same with dimensions (and only support up to 4 dimensions). This was kind of my worst case scenario as it would make the code a lot longer). – Sean Peters Jan 15 '13 at 10:10
  • @user1841643 well there you have it: It's a dangerous thing to do, but you probably can't get around casting your array to void* and remembering the correct dimension while casting it back. – stefan Jan 15 '13 at 10:16

2 Answers2

2

The short answer is that you cannot declare such an array in C++. The dimensions of an array are part of the type (with a miscellaneous exception that sometimes the value of one of the dimensions can be unknown, for an extern array declaration). The number of dimensions is always part of the type, and the type must be known at compile time.

What you might be able to do instead is to use a "flat" array of appropriate size. For example, if you need a 3x3...x3 array then you can compute 3^n at runtime, dynamically allocate that many int (probably using a vector<int> for convenience), and you have memory with the same layout as an int[3][3]...[3]. You can refer to this memory via a void*.

I'm not certain that it's strictly legal in C++ to alias a flat array as a multi-dimensional array. But firstly the function you're calling might not actually alias it that way anyway given that it also doesn't know the dimension at compile-time. Secondly it will work in practice (if it doesn't, the function you're calling is either broken or else has some cunning way to deal with this that you should find out about and copy).

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
-2

You can't use array in this case. Array is only for those data whose size and dimension are known at compile time. Try use an array of std::vector instead

someone_ smiley
  • 1,006
  • 3
  • 23
  • 42
  • 2
    Darn, the API only accepts a buffer of array type and of dimensions equal to the number of dimensions in the image. So I don't think I can use a vector. – Sean Peters Jan 15 '13 at 10:10
  • in that case there is hardly anything we can do as "buffer of array" type seems compile time info to me. btw,which API are you talking about? – someone_ smiley Jan 15 '13 at 10:16
  • Sorry I think I was a little ambiguous. The buffer is of type "void *", the specific type really depends on the image (it could be anything from float*** to short*). The library is HDF5. Link to the specific method in question [link](http://www.hdfgroup.org/HDF5/doc/RM/RM_H5D.html#Dataset-Read) – Sean Peters Jan 15 '13 at 10:21