Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic multidimensional array 1

Status
Not open for further replies.

Arainxius

Technical User
Sep 11, 2003
16
0
0
CA
I need to read from the file and depending upon the three values I need a dynamic array that is something like

int array3D [frame][height][width]

I don't know the three parameters(frame, height, width) at compile time.


Then i want to read the datafile frame by frame
fread(df, sizeof(int), (height*width), fp)

and then assign it to
array3D[0][0][0] to array3D[0][height][width]

and then read second frame and assign to
array3D[1][0][0] to array3D[1][height][width]

and so on...

any help

thanks


 
Use a vector:
Code:
typedef vector<int>           intVector_t;
typedef vector<intVector_t>   intVector2D_t;
typedef vector<intVector2D_t> intVector3D_t;

intVector3D_t array3D;

for ( int frame = 0; frame < maxFrames; ++frame )
{
   intVector2D_t array2D;

   for ( int height = 0; height < maxHeight; ++height )
   {
      intVector_t array;

      for ( int width = 0; width < maxWidth; ++width )
      {
         // Read num from your file...

         array.push_back( num );  // Add num to end of array.
      }

      array2D.push_back( array );  // Add array to end of 2D array.
   }

   array3D.push_back( array2D );  // Add 2D array to end of 3D array;
}
 
I just tried to compile the above code and it gives the following error

10 .typedef vector <int> intVector_t;
11. typedef vector<intVector_t> intVector2D_t;
12. typedef vector<intVector2D_t> intVector3D_t;

1>c:\temp\testarry\testarry\testarry.cpp(10) : error C2143: syntax error : missing ';' before '<'
1>c:\temp\testarry\testarry\testarry.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\temp\testarry\testarry\testarry.cpp(11) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
1>c:\temp\testarry\testarry\testarry.cpp(11) : error C2143: syntax error : missing ';' before '<'
1>c:\temp\testarry\testarry\testarry.cpp(11) : error C2143: syntax error : missing ';' before '<'
1>c:\temp\testarry\testarry\testarry.cpp(12) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
1>c:\temp\testarry\testarry\testarry.cpp(12) : error C2143: syntax error : missing ';' before '<'
1>c:\temp\testarry\testarry\testarry.cpp(12) : error C2143: syntax error : missing ';' before '<'

If it works then will i be able to access the individual data as array3D[j][k] ??

thanks

 
Did you add these lines to your code:
Code:
#include <vector>
// your other #include lines...

using namespace std;
...

Yes, you can read the data like a normal array, but you must use the vector methods like push_back() to add data to it.

Here's the MSDN documentation for the STL vector class:
 
This is perfect. thanks.

However by using vectors the performance is very poor.
Code takes too much to execute.
 
Arainxius said:
However by using vectors the performance is very poor.
No. It is possible to use the vector in ways that lead to poor performance, but in general it will be roughly the same as a similar implementation with C style dynamic arrays. It does have the benefit of being safer and less bug prone, which is why you generally use vectors instead of dynamic arrays in C++.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top