code_junkie

How do I best handle dynamic multi-dimensional arrays in C/C++?

Nov 12th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. public static void main(String[] args){
  2. Scanner sc=new Scanner(System.in);
  3. int rows=sc.nextInt();
  4. int cols=sc.nextInt();
  5. int[][] data=new int[rows][cols];
  6. manipulate(data);
  7. }
  8.  
  9. public static void manipulate(int[][] data){
  10. for(int i=0;i<data.length;i++)
  11. for(int j=0;j<data[0].length.j++){
  12. System.out.print(data[i][j]);
  13. }
  14. }
  15.  
  16. #include "boost/multi_array.hpp"
  17. #include <cassert>
  18.  
  19. int
  20. main () {
  21. // Create a 3D array that is 3 x 4 x 2
  22. typedef boost::multi_array<double, 3> array_type;
  23. typedef array_type::index index;
  24. array_type A(boost::extents[3][4][2]);
  25.  
  26. // Assign values to the elements
  27. int values = 0;
  28. for(index i = 0; i != 3; ++i)
  29. for(index j = 0; j != 4; ++j)
  30. for(index k = 0; k != 2; ++k)
  31. A[i][j][k] = values++;
  32.  
  33. // Verify values
  34. int verify = 0;
  35. for(index i = 0; i != 3; ++i)
  36. for(index j = 0; j != 4; ++j)
  37. for(index k = 0; k != 2; ++k)
  38. assert(A[i][j][k] == verify++);
  39.  
  40. return 0;
  41. }
  42.  
  43. Multi-Array test!
  44. Please enter the size of the dimension 0 : 4
  45.  
  46. Please enter the size of the dimension 1 : 6
  47.  
  48. Please enter the size of the dimension 2 : 2
  49.  
  50. Text matrix with 3 dimensions of size (4,6,2) have been created.
  51.  
  52. Ready!
  53. Type 'help' for the command list.
  54.  
  55. >read 0.0.0
  56. Text at (0,0,0) :
  57. ""
  58.  
  59. >write 0.0.0 "This is a nice test!"
  60. Text "This is a nice test!" written at position (0,0,0)
  61.  
  62. >read 0.0.0
  63. Text at (0,0,0) :
  64. "This is a nice test!"
  65.  
  66. >write 0,0,1 "What a nice day!"
  67. Text "What a nice day!" written at position (0,0,1)
  68.  
  69. >read 0.0.0
  70. Text at (0,0,0) :
  71. "This is a nice test!"
  72.  
  73. >read 0.0.1
  74. Text at (0,0,1) :
  75. "What a nice day!"
  76.  
  77. >write 3,5,1 "This is the last text!"
  78. Text "This is the last text!" written at position (3,5,1)
  79.  
  80. >read 3,5,1
  81. Text at (3,5,1) :
  82. "This is the last text!"
  83.  
  84. >exit
  85.  
  86. const unsigned int DIMENSION_COUNT = 3; // dimension count for this test application, change it at will :)
  87.  
  88. // here is the type of the multi-dimensional (DIMENSION_COUNT dimensions here) array we want to use
  89. // for this example, it own texts
  90. typedef boost::multi_array< std::string , DIMENSION_COUNT > TextMatrix;
  91.  
  92. // this provide size/index based position for a TextMatrix entry.
  93. typedef std::tr1::array<TextMatrix::index, DIMENSION_COUNT> Position; // note that it can be a boost::array or a simple array
  94.  
  95. /* This function will allow the user to manipulate the created array
  96. by managing it's commands.
  97. Returns true if the exit command have been called.
  98. */
  99. bool process_command( const std::string& entry, TextMatrix& text_matrix );
  100.  
  101. /* Print the position values in the standard output. */
  102. void display_position( const Position& position );
  103.  
  104. int main()
  105. {
  106. std::cout << "Multi-Array test!" << std::endl;
  107.  
  108. // get the dimension informations from the user
  109. Position dimensions; // this array will hold the size of each dimension
  110.  
  111. for( int dimension_idx = 0; dimension_idx < DIMENSION_COUNT; ++dimension_idx )
  112. {
  113. std::cout << "Please enter the size of the dimension "<< dimension_idx <<" : ";
  114. // note that here we should check the type of the entry, but it's a simple example so lets assume we take good numbers
  115. std::cin >> dimensions[dimension_idx];
  116. std::cout << std::endl;
  117.  
  118. }
  119.  
  120. // now create the multi-dimensional array with the previously collected informations
  121. TextMatrix text_matrix( dimensions );
  122.  
  123. std::cout << "Text matrix with " << DIMENSION_COUNT << " dimensions of size ";
  124. display_position( dimensions );
  125. std::cout << " have been created."<< std::endl;
  126. std::cout << std::endl;
  127. std::cout << "Ready!" << std::endl;
  128. std::cout << "Type 'help' for the command list." << std::endl;
  129. std::cin.sync();
  130.  
  131.  
  132. // we can now play with it as long as we want
  133. bool wants_to_exit = false;
  134. while( !wants_to_exit )
  135. {
  136. std::cout << std::endl << ">" ;
  137. std::tr1::array< char, 256 > entry_buffer;
  138. std::cin.getline(entry_buffer.data(), entry_buffer.size());
  139.  
  140. const std::string entry( entry_buffer.data() );
  141. wants_to_exit = process_command( entry, text_matrix );
  142. }
  143.  
  144. return 0;
  145. }
  146.  
  147. void write_in_text_matrix( TextMatrix& text_matrix, const Position& position, const std::string& text )
  148. {
  149. text_matrix( position ) = text;
  150. std::cout << "Text "" << text << "" written at position ";
  151. display_position( position );
  152. std::cout << std::endl;
  153. }
  154.  
  155. void read_from_text_matrix( const TextMatrix& text_matrix, const Position& position )
  156. {
  157. const std::string& text = text_matrix( position );
  158. std::cout << "Text at ";
  159. display_position(position);
  160. std::cout << " : "<< std::endl;
  161. std::cout << " "" << text << """ << std::endl;
  162. }
  163.  
  164. // First dimension
  165. int** array = new int*[3];
  166. for( int i = 0; i < 3; ++i )
  167. {
  168. // Second dimension
  169. array[i] = new int[4];
  170. }
  171.  
  172. // You can then access your array data with
  173. for( int i = 0; i < 3; ++i )
  174. {
  175. for( int j = 0; j < 4; ++j )
  176. {
  177. std::cout << array[i][j];
  178. }
  179. }
  180.  
  181. int* buffer = new int[3*4];
  182. int** array = new int*[3];
  183.  
  184. for( int i = 0; i < 3; ++i )
  185. {
  186. array[i] = array + i * 4;
  187. }
  188.  
  189. // You can then access your array data with
  190. for( int i = 0; i < 3; ++i )
  191. {
  192. for( int j = 0; j < 4; ++j )
  193. {
  194. const int index = i * 4 + j;
  195. std::cout << buffer[index];
  196. }
  197. }
  198.  
  199. void *Array_get_2d(Array_T a, int width, int height, int i, int j) {
  200. return Array_get(a, j * width, i, j);
  201. }
  202.  
  203. std::vector< std::vector<int> > v;
  204. v.resize(rows, std::vector<int>(cols, 42)); // init value is 42
  205. v[row][col] = ...;
  206.  
  207. std::vector<int> v(col_count * row_count, 42);
  208. v[col_count * row + col) = ...;
  209.  
  210. int ** rows = new int*[row_count];
  211. for(std::size_t i = 0; i < row_count; i++) {
  212. rows[i] = new int[cols_count];
  213. std::fill(rows[i], rows[i] + cols_count, 42);
  214. }
  215.  
  216. // use it... rows[row][col] then free it...
  217.  
  218. for(std::size_t i = 0; i < row_count; i++) {
  219. delete[] rows[i];
  220. }
  221.  
  222. delete[] rows;
  223.  
  224. int array [ rows ] [ columns ];
  225.  
  226. int array [ rows * columns ]
  227.  
  228. void * array = malloc ( rows * columns * sizeof(int) );
  229.  
  230. int main( int argc, char ** argv )
  231. {
  232. UASSERT( argc, >, 2 ); // My own macro...
  233.  
  234. int rows = atoi( argv[1] );
  235. int columns = atoi( argv[2] );
  236.  
  237. int data [ rows ] [ columns ]; // Yes, legal!
  238.  
  239. memset( data, 0, sizeof(data) );
  240.  
  241. print( data, rows, columns );
  242. manipulate( data, rows, columns );
  243. print( data, rows, columns );
  244. }
  245.  
  246. void manipulate( void * theRealData, int theRows, int theColumns )
  247. {
  248. int (* p) [ theRows ] [ theColumns ];
  249. *(void **)&p = theRealData; //Need *&. Issue with casting lvalues.
  250.  
  251. for ( int r = 0; r < theRows; r ++ )
  252. for ( int c = 0; c < theColumns; c ++ )
  253. (*p) [r] [c] = r*10 + c;
  254. }
  255.  
  256. void print( void *theRealData, int theRows, int theColumns )
  257. {
  258. int (* p) [ theRows ] [ theColumns ];
  259. *(void **)&p = theRealData; //Need *&. Issue with casting lvalues.
  260.  
  261. for ( int r = 0; r < theRows; r ++ )
  262. {
  263. for ( int c = 0; c < theColumns; c ++ )
  264. {
  265. cout << (*p) [r] [c] << " ";
  266. }
  267. cout << endl;
  268. }
  269. cout << endl;
  270. }
  271.  
  272. main()
  273. {
  274. int i;
  275. int rows;
  276. int cols;
  277. int **array = NULL;
  278.  
  279. array = malloc(sizeof(int*) * rows);
  280. if (array == NULL)
  281. return 0; // check for malloc fail
  282.  
  283. for (i = 0; i < rows; i++)
  284. {
  285. array[i] = malloc(sizeof(int) * cols)
  286. if (array[i] == NULL)
  287. return 0; // check for malloc fail
  288. }
  289.  
  290. // and now you have a dynamically sized array
  291. }
Add Comment
Please, Sign In to add comment