These are just some of the Arrays sizes i've used, many of them are inside a loop.


int ss[80000]={0};
char pres[64000];

int ith[4096][16]={0},ich[4096]={0},pres2[2500][16]={0};


int slf[4096][16]={0},slr[4096]={0},rh=0,crs[4096]={0},cri=0,moo=0,nb=0;
int qlf[4096][16]={0},wlf[4096][16]={0},kd=0;
 
      
int jlf[4096][16]={0},jlr[4096]={0},jrs[4096]={0},jri=0;
int klf[4096][16]={0},klr[4096]={0},krs[1204]={0},kri=0;


int vlf[4096][16]={0},vlr[4096]={0},vrs[4096]={0},vri=0;
int plf[4096][16]={0},plr[4096]={0},prs[4096]={0},pri=0;

80% of the time my program runs properly but every other time it suddenly stops executing and closes the program unexpectedly and after debugging i always find out that one of the counters for the array has gone way off the limit, like ith[4096][16] will have values uptil ith[6400(counter)][16].

I also can't declare the size as a high upper limit of 6400 as the program refuses to run if i do this with every array. I thought about using Dynamic array but i've never used them before and am not sure of the right way to do it. I"ve seen a few examples online but my problem is the counter variable increases in different parts of the program including loops while processing the ith[][] variable itself it may increase its size, how do i define it then

int * ich;
ich = new int [Counter];

What about Multi-dimensional pointers with one fixed varaible ?

int ** ith;
ith = new int [Counter][16];

or 

ith =new int*[counter];
for(int i=0; i<16;i++){
ith[i]=new int[16];}

Are there better ways of solving my problem other than dyanmic array ? Can i constantly change the size of the arrar by using Dynamic allocation ?

What about Vectars ? Is it relatively easier to implement for an amateur?

Remember, arrays are initialized with a strict size --unless they are dynamic. To access the memory for any array, you access it be 0 through one less than the total size.

int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// numbers[0] = 1;
// numbers[9] = 10;
// numbers[10] = invalid due to accessing past the allocated memory - program will likely exit or give junk result

std::vectors aren't strictly needed, but they do take care of the dynamic memory allocation for you, and tend to do so in a reasonably efficient manner.

I'm curious though about what your application is, that it requires several large 2-dimensional arrays, and whether it would be worthwhile or appropriate to use a struct (or class, since this is the C++ development forum):

// instead of
int slf[4096][16]={0},slr[4096]={0},rh=0,crs[4096]={0},cri=0,moo=0,nb=0;
int qlf[4096][16]={0},wlf[4096][16]={0},kd=0;

// use the following
struct Datapoint {
    int slf[16], slr, crs;
    int qlf[16], wlf[16];
};
Datapoint p[4096];
int rh=0, cri=0, moo=0, nb=0;
int kd=0;

if, in fact, all of those variables (or more) are related in a way that it makes sense to group them.

... i always find out that one of the counters for the array has gone way off the limit, like ith[4096][16] will have values uptil ith[6400(counter)][16].

This has to do with the order in which you declare your arrays (that there is additional space after "ith" allocated for other arrays, so you can still index into that space), but why are you looking at ith[6400][16] if you only declared it up to [4096][16]? If there weren't other variables declared, you probably would have incurred a stack error and/or segment violation for trying to look into memory that didn't belong to your process.

Even for an expert, multi-D dynamic array accessing still writes lot lines.
But they know how to simplify it.

It is very easy to create,resize,and close a 1-D dynamic array with
malloc(),realloc() and free()
see
http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/
http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/
http://www.cplusplus.com/reference/clibrary/cstdlib/free/

and use a calculation I call it "D-Bounce" to use it as multi-D dynamic array.
it works like this:

int arr[13][17][19];
arr[3][11][7]=7;
//can be replaced with
int*arr=(int*)malloc(13*17*19*sizeof(int))
arr[ 3*17 + 11*19 + 7 ]=7;

int arr[13][17][19];
arr[3][11][7]=7;
//can be replaced with
int*arr=(int*)malloc(13*17*19*sizeof(int))
arr[ 3*17 + 11*19 + 7 ]=7;

Hmmm, I think that indexing scheme should read:

arr[ 3*17*19 + 11*19 + 7 ] = 7;

Another advantage is that if you're frequently moving to an adjacent element, you can do things like:

int updown_offset = 17*19;
int northsouth_offset = 19;
int eastwest_offset = 1;

int *ptr = &arr[ 0*17*19 + 11*19 + 7 ];  // or whatever starting position
for (int updown=0; updown<13; updown++) {
    *ptr = 7;
    ptr += updown_offset;
}

Not really as readable as arr[k][11][7] = 7; , but the latter would require 3 adds and 3 pointer dereferences for each time through the loop, instead of 1 add and 1 dereference (and of course an add and a compare for the loop variable either way). It starts to matter when you're doing heavy math across every element in your multi-dimensional array!

This article has been dead for over six months. Start a new discussion instead.