Array Size Problems

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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?[/font][/size][/size]
closed account (zb0S216C)
cppdeveloper wrote:
Can i constantly change the size of the arrar by using Dynamic allocation ? (sic)

The size of a dynamically allocated array can be determined at run-time (user-defined length) or during compile time. With non-dynamically allocated arrays, their lengths need to be defined during compile time.

The problem with large arrays (such as those you defined) is that your run the risk of a stack-overflow. Remember that an stack can only hold 1024 kilobytes (1MB) of data. With dynamic arrays, you run the risk of memory leaks and invalid pointers if not used correctly.

Here's my suggestion: Dynamically allocate your array if the stack can't handle it. Otherwise, use the stack (faster). Given your code in the original post, I would definitely suggest dynamic allocation.

Wazzak
Last edited on
Remember that an stack can only hold 1024 bytes of data

Not sure where the number of 1024 bytes comes from. Did you mean 1024 kbytes? (that's the default stack size for VC++). I regularly use char buffers of 1024 elements, in addition to other variables, with no problem.

But the code above is certainly putting far too much on the stack for comfort. If I've done my sums right, it's using 2826228 bytes, which is well on the way to 3 Mbytes (3 x 1048576 = 3145728)

Visual Studio's analysis tool warns you if your stack usage for a single function exceeds 16kbytes (C6262). While this is not an actual limit, it is a bad idea to exceed it. Here the limit has been totally blown out of the water!

Andy

P.S. Out of interest, what compiler were you using?
Last edited on
closed account (zb0S216C)
andywestken wrote:
Did you mean 1024 kbytes? (sic)

It was meant to be 1MB. I'll change it. Thanks for pointing it out :)

andywestken wrote:
I regularly use char buffers of 1024

1KB. The stack defaults at 1MB.

Wazzak
Last edited on
Topic archived. No new replies allowed.