title: How do I best handle dynamic multi-dimensional arrays in C/C++?
body: <p>What is the accepted/most commonly used way to manipulate dynamic (with all dimensions not known until runtime) multi-dimensional arrays in C and/or C++.</p>
  
  <p>I'm trying to find the cleanest way to accomplish what this Java code does:</p>
  
  <pre><code>public static void main(String[] args){
   Scanner sc=new Scanner(System.in);
   int rows=sc.nextInt();
   int cols=sc.nextInt();
   int[][] data=new int[rows][cols];
   manipulate(data);
  }
  
  public static void manipulate(int[][] data){
     for(int i=0;i&lt;data.length;i++)
     for(int j=0;j&lt;data[0].length.j++){
           System.out.print(data[i][j]);       
     }    
  }
  </code></pre>
  
  <p>(reads from std_in just to clarify that dimensions aren't known until runtime).</p>
  
  <p>Edit:I noticed that this question is pretty popular even though it's pretty old.  I don't actually agree with the top voted answer.   I think the best choice for C is to use a single-dimensional array as Guge said below "You can alloc rows<em>cols</em>sizeof(int) and access it by table[row*cols+col].".</p>
  
  <p>There is a number of choices with C++, if you really like boost or stl then the answers below might be preferable, but the simplest and probably fastest choice is to use a single dimensional array as in C.</p>
  
  <p>Another viable choice in C and C++ if you want the [][] syntax is lillq's answer down at the bottom is manually building the array with lots of malloc's.</p>