1

I want to have a large 2 dimensional array such like

int myArray[10000][2];

I was told that the array built in such way is not appropriate, and should use malloc to build in heap. Could someone show me how to accomplish this? thanks!

eastboundr
  • 1,857
  • 8
  • 28
  • 46
  • possible duplicate of [Declaring a 2-dimensional array of unknown size, C](http://stackoverflow.com/questions/3089731/declaring-a-2-dimensional-array-of-unknown-size-c) – Oliver Charlesworth Apr 04 '12 at 08:03
  • Duplicate of [c-malloc-for-two-dimensional-array](http://stackoverflow.com/questions/1970698/c-malloc-for-two-dimensional-array) – Pavan Manjunath Apr 04 '12 at 08:04
  • You may want to have a look at [comp.lang.c FAQ list · Question 6.16: How can I dynamically allocate a multidimensional array?](http://c-faq.com/aryptr/dynmuldimary.html) – Mackie Messer Apr 04 '12 at 09:02

1 Answers1

1
#include <stdlib.h>

//alloc

int **vv = malloc(2 * sizeof(int *));
for(int i = 0; i < 2; i++)
   vv[i] = malloc(10000 * sizeof(int));

//free

for(int i = 0; i < 2; i++)
    free(vv[i]);
free(vv);
jacekmigacz
  • 789
  • 12
  • 22
  • Hi jace, thanks. So should the array be referred as vv or vv2? because you have *vv2, then vv[i]. And it's vv, could I used vv[0][0] to refer to the first slot then? Thanks – eastboundr Apr 04 '12 at 08:17
  • addresses range: vv[0][0] - vv[1][9999] are valid – jacekmigacz Apr 04 '12 at 08:22