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

``````int ss={0};
char pres;

int ith={0},ich={0},pres2={0};

int slf={0},slr={0},rh=0,crs={0},cri=0,moo=0,nb=0;
int qlf={0},wlf={0},kd=0;

int jlf={0},jlr={0},jrs={0},jri=0;
int klf={0},klr={0},krs={0},kri=0;

int vlf={0},vlr={0},vrs={0},vri=0;
int plf={0},plr={0},prs={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 will have values uptil ith[6400(counter)].

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];

or

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

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?

## Recommended Answers

Use std::vector<> instead of raw arrays.

For a beginner, a vector is easier to use (correctly).

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 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// numbers = …``````

## All 5 Replies

Use std::vector<> instead of raw arrays.

For a beginner, a vector is easier to use (correctly).

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 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// numbers = 1;
// numbers = 10;
// numbers = 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={0},slr={0},rh=0,crs={0},cri=0,moo=0,nb=0;
int qlf={0},wlf={0},kd=0;

// use the following
struct Datapoint {
int slf, slr, crs;
int qlf, wlf;
};
Datapoint p;
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 will have values uptil ith[6400(counter)].

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 if you only declared it up to ? 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;
arr=7;
//can be replaced with
int*arr=(int*)malloc(13*17*19*sizeof(int))
arr[ 3*17 + 11*19 + 7 ]=7;

int arr;
arr=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] = 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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.