How to create a large 2-dimensional array with C

Go To StackoverFlow.com

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!

2012-04-04 08:02
by eastboundr
possible duplicate of Declaring a 2-dimensional array of unknown size, COliver Charlesworth 2012-04-04 08:03
Duplicate of c-malloc-for-two-dimensional-arrayPavan Manjunath 2012-04-04 08:04


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);
2012-04-04 08:05
by jacekmigacz
Is a single pointer enough - Pavan Manjunath 2012-04-04 08:06
here you go... version with double pointer - jacekmigacz 2012-04-04 08:12
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? Thank - eastboundr 2012-04-04 08:17
addresses range: vv[0][0] - vv[1][9999] are vali - jacekmigacz 2012-04-04 08:22
Thanks. It work - eastboundr 2012-04-04 08:28
Ads