I am writing a C program using Dev-CPP. It uses a 3 dimensional array to store around 350 values as shown below.

db[2][0][1]=3278;
db[2][0][4]=2368;
db[3][0][3]=6686;
db[2][0][6]=7224;
..
..
..

I get a valid output when I read the array with the following code

zone = db[2][0][1];
printf("%d",zone);

but it stops abruptly with a error msg when i run a code like this...

int h,t,o;
h=2;
t=0;
o=1;
zone = db[h][t][o];

The program stops exactly when the last line is executed...:sad: . It get compiled without any error

help me to read the array using the int values...

I am sorry. I should have done that first..

int db[9][9][9];

the maximum value is 7789 and the minimum value is 3278.

The values you put in the array are not relevent to your problem. I suspect the actual error is elsewhere in your program because what you posted looks ok. Without more code its impossible to say what your problem is.

Either that, or you have some miniscule amount of RAM in your computer. What's your compiler? It's highly unlikely, but a crappy old compiler has much different limits than a modern one.

Hello there.

Either that, or you have some miniscule amount of RAM in your computer. What's your compiler? It's highly unlikely, but a crappy old compiler has much different limits than a modern one.

Yes, an old compiler imposes a limit on the amount of memory that can be allocated to a single variable, but if that were the senario, the program execution would have halted at the point he has declared the three dimensional array.

I hope you realize that even though he hasn't stored any values initially in his array, the space has been nonetheless allocated to him. I don't think any failed attempt after the allocation is due to the old compiler.

And as far as RAM is concerned, he is allocating 729 * 4 bytes ~ 2.85 KB. I don't think this should be as such a problem...;)

Thank you.

Comments
Correct you are :). -joeprogrammer

Hello there.

Yes, an old compiler imposes a limit on the amount of memory that can be allocated to a single variable, but if that were the senario, the program execution would have halted at the point he has declared the three dimensional array.

I hope you realize that even though he hasn't stored any values initially in his array, the space has been nonetheless allocated to him. I don't think any failed attempt after the allocation is due to the old compiler.

And as far as RAM is concerned, he is allocating 729 * 4 bytes ~ 2.85 KB. I don't think this should be as such a problem...;)

You're right - I guess I was thinking in terms of dynamic memory allocation...

Ancient Dragon is right - there's an error somewhere else in your program. Post your entire code so that we can criticize... I mean, correct, your code. :cheesy:

Hello there.

You're right - I guess I was thinking in terms of dynamic memory allocation...

Ah..just to clear the matters up...even if we dynamically allocated 2.9KB of memory, it wouldn't be such a problem, unless of course, you were doing realloc i.e. allocating memory during runtime and exceeding the limitations imposed by the current memory model of the compiler.

Thank you.

here is the code.. so that you could correct....
this is a program to find time zone from area code...

#include <stdio.h>
#include <stdlib.h>
 
void db_init(void);/*function to initialise db[9][9][9] to zero*/
void db_init1(void);/*function to initialise db[9][9][9] to with the database*/
int db[9][9][9]; /*array to store the value*/
int n;
int zone;
int main()
{
db_init();/*function to initialise db[9][9][9] to zero*/
db_init1();/*function to initialise db[9][9][9] to with the database*/
int hundred,ten,one;
int query[100];/*array to get input from user*/
int filtered[100];/*array to store the only the number in the input from user*/
int i=0,j,k;
 
for(i=0;i<=100;i++)/*function to initialise filtered to zero*/
filtered[i]=0;
i=0;
 
 
printf("Hi! Thank you for using Siva's time zone locator.\nPress any key to continue....");
getch();
zone = db[2][0][1];/*not part of program. only to check*/
printf("%d",zone);/*not part of program. only to check*/
getch();
system("CLS");
 
 
printf("Please enter upto 20 three digit area codes and type \\ and hit enter\n"); 
 
/* \ is to indicate end of input*/
 
 
 
while(i!=100 && (query[i]=getchar())!=92)/*to get input from user*/
{
++i;
}
 
 
n=i-1;/* -1 to exclude the \ at the end of the array*/
k=0;
for(i=0;i<=n;i++)
{
j=query[i];
if (j>47 && j<58)/* to move the element in query[] to filtered[] if its a number*/
{
filtered[k]=j;
++k;
}
else
;
}
for (i=0;i<=k;i)
{
hundred=filtered[i];
ten=filtered[i+1];
one=filtered[i+2];
 
putchar(hundred);
putchar(ten);
putchar(one);/*works great till here*/
zone = db[hundred][ten][one];/*problem starts here*/
 
switch (zone)
{
case 3278:
printf("\tEastern Standard Time Zones");
case 2368:
printf("\tCentral Standard Time Zones");
case 6686:
printf("\tMountain Standard Time Zones");
case 7224:
printf("\tPacific Standard Time Zones");
case 2389:
printf("\tEastern & Central Time Zones");
case 2689:
printf("\tCentral & Mountain Time Zones");
case 6289:
printf("\tMountain & Central Time Zones");
case 6789:
printf("\tMountain & Pacific Time Zones");
case 2289:
printf("\tAleutian & Alaskan Time Zones"); 
case 2852:
printf("\tAtlantic Time Zones"); 
case 6389:
printf("\tNewfoundland Time Zone (Canadian)");
case 7289:
printf("\tPacific & Alaskan Time Zones");
case 7789:
printf("\tSamoan Standard Time Zones");
default:
printf("invalid code");
}
 
i=i+3;
}
 
 
 
 
 
return 0;
 
 
}
void db_init(void)
{
/*function to initialise db[9][9][9] to zero*/
/*using a seperate function because programs stops after this if used in main()*/ 
int i,j,k;
for (i=0;i<=9;i++)
{
for (j=0;j<=9;j++)
{
for (k=0;k<=9;k++)
{
db[i][j][k]=0;
}
}
}
 
}
void db_init1(void)
{
/*function to initialise db[9][9][9] to with the database*/
/*using a seperate function because programs stops after this if used in main()*/ 
db[2][0][1]=3278;/*Eastern Time Zone*/ 
db[2][0][2]=3278;
db[2][0][3]=3278;
db[2][0][7]=3278;
db[2][1][2]=3278;
db[2][1][5]=3278;
db[2][1][6]=3278;
db[2][2][6]=3278;
db[2][2][9]=3278;
db[2][3][1]=3278;
db[2][3][4]=3278;
db[2][3][9]=3278;
db[2][4][0]=3278;
db[2][4][8]=3278;
db[2][5][2]=3278;
db[2][6][7]=3278;
db[2][6][9]=3278;
db[2][7][6]=3278;
db[2][8][9]=3278;
db[3][0][1]=3278;
db[3][0][2]=3278;
db[3][0][4]=3278;
db[3][0][5]=3278;
db[3][1][3]=3278;
db[3][1][5]=3278;
db[3][2][1]=3278;
db[3][3][0]=3278;
db[3][3][6]=3278;
db[3][3][9]=3278;
db[3][4][5]=3278;
db[3][4][7]=3278;
db[3][5][1]=3278;
db[3][5][2]=3278;
db[3][8][6]=3278;
db[4][0][1]=3278;
db[4][0][4]=3278;
db[4][0][7]=3278;
db[4][1][0]=3278;
db[4][1][2]=3278;
db[4][1][3]=3278;
db[4][1][6]=3278;
db[4][1][8]=3278;
db[4][1][9]=3278;
db[4][3][4]=3278;
db[4][3][8]=3278;
db[4][4][0]=3278;
db[4][4][3]=3278;
db[4][5][0]=3278;
db[4][7][0]=3278;
db[4][7][5]=3278;
db[4][7][8]=3278;
db[4][8][4]=3278;
db[5][0][2]=3278;
db[5][0][8]=3278;
db[5][1][3]=3278;
db[5][1][4]=3278;
db[5][1][6]=3278;
db[5][1][7]=3278;
db[5][1][8]=3278;
db[5][1][9]=3278;
db[5][4][0]=3278;
db[5][5][1]=3278;
db[5][6][1]=3278;
db[5][6][7]=3278;
db[5][7][0]=3278;
db[5][7][1]=3278;
db[5][8][5]=3278;
db[5][8][6]=3278;
db[6][0][3]=3278;
db[6][0][6]=3278;
db[6][0][7]=3278;
db[6][0][9]=3278;
db[6][1][0]=3278;
db[6][1][3]=3278;
db[6][1][4]=3278;
db[6][1][6]=3278;
db[6][1][7]=3278;
db[6][3][1]=3278;
db[6][4][6]=3278;
db[6][4][7]=3278;
db[6][4][9]=3278;
db[6][7][8]=3278;
db[7][0][3]=3278;
db[7][0][4]=3278;
db[7][0][5]=3278; 
db[7][0][6]=3278;
db[7][1][6]=3278;
db[7][1][7]=3278;
db[7][1][8]=3278;
db[7][2][4]=3278;
db[7][2][7]=3278;
db[7][3][2]=3278;
db[7][3][4]=3278;
db[7][4][0]=3278;
db[7][5][4]=3278;
db[7][5][7]=3278;
db[7][7][0]=3278;
db[7][7][2]=3278;
db[7][7][4]=3278;
db[7][8][1]=3278;
db[7][8][6]=3278;
db[8][0][2]=3278;
db[8][0][3]=3278;
db[8][0][4]=3278;
db[8][1][0]=3278;
db[8][1][3]=3278;
db[8][1][4]=3278;
db[8][1][9]=3278;
db[8][2][8]=3278;
db[8][3][5]=3278;
db[8][4][3]=3278;
db[8][4][5]=3278;
db[8][4][8]=3278;
db[8][5][6]=3278;
db[8][5][7]=3278;
db[8][5][9]=3278;
db[8][6][0]=3278;
db[8][6][2]=3278;
db[8][6][3]=3278;
db[8][6][5]=3278;
db[8][7][6]=3278;
db[8][7][8]=3278;
db[9][0][4]=3278;
db[9][0][5]=3278;
db[9][0][8]=3278;
db[9][1][0]=3278;
db[9][1][2]=3278;
db[9][1][4]=3278;
db[9][1][7]=3278;
db[9][1][9]=3278;
db[9][3][7]=3278;
db[9][4][1]=3278;
db[9][4][7]=3278;
db[9][5][4]=3278;
db[9][5][9]=3278;
db[9][7][3]=3278;
db[9][7][8]=3278;
db[9][8][0]=3278;
db[9][8][9]=3278;
db[2][0][4]=2368;/*Central Time Zone */
db[2][0][5]=2368;
db[2][1][0]=2368;
db[2][1][4]=2368;
db[2][1][7]=2368;
db[2][1][8]=2368;
db[2][2][4]=2368;
db[2][2][5]=2368;
db[2][2][8]=2368;
db[2][5][1]=2368;
db[2][5][4]=2368;
db[2][5][6]=2368;
db[2][6][2]=2368;
db[2][7][0]=2368;
db[2][8][1]=2368;
db[3][0][9]=2368;
db[3][1][2]=2368;
db[3][1][4]=2368;
db[3][1][6]=2368;
db[3][1][8]=2368;
db[3][1][9]=2368;
db[3][2][0]=2368;
db[3][2][5]=2368;
db[3][3][4]=2368;
db[3][3][7]=2368;
db[3][6][1]=2368;
db[4][0][2]=2368;
db[4][0][5]=2368;
db[4][0][9]=2368;
db[4][1][4]=2368;
db[4][1][7]=2368;
db[4][3][0]=2368;
db[4][3][2]=2368;
db[4][6][9]=2368;
db[4][7][9]=2368;
db[5][0][1]=2368;
db[5][0][4]=2368;
db[5][0][7]=2368;
db[5][1][2]=2368;
db[5][1][5]=2368;
db[5][6][3]=2368;
db[5][7][3]=2368;
db[5][8][0]=2368;
db[6][0][1]=2368;
db[6][0][8]=2368;
db[6][1][2]=2368;
db[6][1][5]=2368;
db[6][1][8]=2368;
db[6][3][0]=2368;
db[6][3][6]=2368;
db[6][4][1]=2368;
db[6][5][1]=2368;
db[6][6][0]=2368;
db[6][6][2]=2368;
db[6][8][2]=2368;
db[7][0][8]=2368;
db[7][1][2]=2368;
db[7][1][3]=2368;
db[7][1][5]=2368;
db[7][3][1]=2368;
db[7][6][3]=2368;
db[7][6][9]=2368;
db[7][7][3]=2368;
db[8][0][6]=2368;
db[8][1][5]=2368;
db[8][1][6]=2368;
db[8][1][7]=2368;
db[8][3][0]=2368;
db[8][3][2]=2368;
db[8][4][7]=2368;
db[8][7][0]=2368;
db[9][0][1]=2368;
db[9][0][3]=2368;
db[9][1][3]=2368;
db[9][1][8]=2368;
db[9][2][0]=2368;
db[9][3][1]=2368;
db[9][3][6]=2368;
db[9][4][0]=2368;
db[9][5][2]=2368;
db[9][5][6]=2368;
db[9][7][2]=2368;
db[9][7][9]=2368;
db[9][8][5]=2368;
db[3][0][3]=6686;/*Mountain Time Zone */
db[3][0][7]=6686;
db[3][8][5]=6686;
db[4][0][3]=6686;
db[4][0][6]=6686;
db[4][3][5]=6686;
db[5][0][5]=6686;
db[7][1][9]=6686;
db[7][2][0]=6686;
db[7][8][0]=6686;
db[8][0][1]=6686;
db[9][7][0]=6686;
db[2][0][6]=7224; /*Pacific Time Zone*/
db[2][0][9]=7224;
db[2][1][3]=7224;
db[2][5][3]=7224;
db[3][1][0]=7224;
db[3][2][3]=7224;
db[3][6][0]=7224;
db[4][0][8]=7224;
db[4][1][5]=7224;
db[4][2][5]=7224;
db[5][0][3]=7224;
db[5][0][9]=7224;
db[5][1][0]=7224;
db[5][3][0]=7224; 
db[5][5][9]=7224;
db[5][6][2]=7224;
db[6][0][4]=7224;
db[6][1][9]=7224;
db[6][2][6]=7224;
db[6][5][0]=7224;
db[6][6][1]=7224;
db[7][0][2]=7224;
db[7][0][7]=7224;
db[7][1][4]=7224;
db[7][6][0]=7224;
db[7][7][5]=7224;
db[7][7][8]=7224;
db[8][0][5]=7224;
db[8][1][8]=7224;
db[8][3][1]=7224;
db[8][5][8]=7224;
db[9][0][9]=7224;
db[9][1][6]=7224;
db[9][2][5]=7224;
db[9][4][9]=7224;
db[9][5][1]=7224;
db[9][7][1]=7224;
db[7][6][5]=2389;/*central and eastern time zone*/
db[2][1][9]=2389;
db[2][6][0]=2389;
db[3][1][7]=2389;
db[4][2][3]=2389;
db[5][7][4]=2389;
db[8][0][7]=2389;
db[8][1][2]=2389;
db[8][5][0]=2389;
db[9][0][6]=2389;
db[6][2][0]=2689;/*Central & Mountain Time Zones*/
db[3][0][6]=2689;
db[3][0][8]=2689;
db[6][0][5]=2689;
db[7][0][1]=6289;/*Mountain & Central Time Zones*/
db[7][8][5]=6289;
db[9][1][5]=6289;
db[2][0][8]=6789;/*Mountain & Pacific Time Zones*/
db[2][5][0]=6789;
db[4][8][0]=6789;
db[5][2][0]=6789;
db[5][4][1]=6789;
db[6][0][2]=6789;
db[6][2][3]=6789;
db[9][2][8]=6789;
db[9][0][7]=2289;/*Aleutian & Alaskan Time Zones*/ 
db[2][4][2]=2852;/*Atlantic Time Zone */
db[2][4][6]=2852;
db[2][6][4]=2852;
db[2][6][8]=2852;
db[2][8][4]=2852;
db[3][4][0]=2852;
db[4][4][1]=2852;
db[4][7][3]=2852;
db[5][0][6]=2852;
db[6][6][4]=2852;
db[7][5][8]=2852;
db[7][6][7]=2852;
db[7][8][4]=2852;
db[7][8][7]=2852;
db[8][0][9]=2852;
db[8][2][9]=2852;
db[8][6][8]=2852;
db[8][6][9]=2852;
db[9][0][2]=2852;
db[9][3][9]=2852;
db[8][0][8]=4292;
db[7][0][9]=6389;/*Newfoundland Time Zone*/
db[8][6][7]=7289;/*Pacific through Alaskan */
db[6][8][4]=7789;/*Samoan Standard Time Zone */
}

I am using dev-c++ ver 4.9.9.2 to develop this program.

> for(i=0;i<=100;i++)
There are many MANY examples of you accessing outside the bounds of your arrays.

more - db[9][7][0]=6686;
and more - db_init()

I compiled and ran the program -- I didn't get any runtime errors, but I only entered a couple time zones.

Your program is more than a little confusing. why are you putting just one character at a time in its own array element of query[] ? For example, if I enter 123 <Enter> the program puts query[0] = '1', query[1] = '2' and query[2] = '3'. Then if I enter 234 <Enter> the program puts query[3] = '2', query[4] = '3' and query[5] = '4'. At this point it has
query[0] = '1'
query[1] = '2'
query[2] = '3'
query[3] = '2'
query[4] = '3'
query[5] = '4'

Note that the above contain the ascii values for the numbers I entered, not the binary values. The letter '1' is NOT the same as the number 1. (google for "ascii chart" and you will discover why)

Next you extract the three values from the above array. '1', '2' and '3' are the first thee. Then you attempt to use them as the index into the db array. But db array has a maximum of 9 for each dimension, and you are attempting to access '1' (49 decimal).

You have an array overflow problem -- attemtping to access elements out of bounds. To correct this you need to convert the letters to binary. One way to do that is like this:

hundred=filtered[i] - '0';
ten=filtered[i+1] - '0';
one=filtered[i+2] - '0';

Finally, you need to put a break statement at the end of each case.

Thank you Salem.

I have changed

int db[9][9][9] to int db[10][10][10]

and

for(i=0;i<=100;i++) to for(i=0;i<=99;i++).

That should fix the boundary error.

But still I get the same error at the same point.

Is there anyother way to read the array instead of

zone = db[hundred][ten][one];

Thank You very much Ancient Dragon. That was more than a help for me...

I knew I was using the ascii code instead of decimal value but I totally forgot it during the computation. I will reply after completing the program.

take care.

It worked!!!!

I changed the ascii value to decimal by subtracting 48 from each element in filtered array.

My first miniproject in C is almost complete..

I will post the final code and file after giving the final touch.

thank you all...

This question has already been answered. Start a new discussion instead.