Segmentation Fault in IF statement inside For Loops

I've been trying to learn C++, and as part of studying, I am building a small program to practice what I learn. I am pretty new to programming.

My problem occurs in an IF statement that sits inside the third level FOR loop in the code below. My program takes what the user typed in and passes it into a function which calls this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void ProcCheckList(){	//Pass information to the main processor. Then, determine output.
	arrlen[1]=25;	//Number of command tests within each type of commands
	arrlen[2]=50;	//Number of string varieties for a command test
	string test[][25][50]={
		{													//Constant Commands
			{/*List of strings*/},
			{/*Another command test*/},
			{/*More strings*/},
			{/*Strings*/}
		},{													//Look at 
			{/*Even more strings*/},
			{/*List of more strings*/},
			{/*Another list of strings*/}
		},{													//Pick-up Commands
			{/*more strings*/},
			{/*More, more strings*/}
		},{													//Room Specific Commands
			{/*More, more string tests*/},
			{/*Yet another list of strings*/},
			{/*More,more strings*/}
		}
	};
	arrlen[0]=GAL(test);									//Number of command types; initialize with a get array length function

	for(int inxx=0; inxx<arrlen[0]; inxx++){				//Test input by comparing it against the strings above.
		for(int indxx=0; inxx<arrlen[1]; inxx++){
			for(int indexx=0; indexx<arrlen[2]; indexx++){
				if(mainword==test[inxx][indxx][indexx]){  //Problem occurs around here
					arrcode[0]=inxx;						//Get an instance of the array by setting an array code.
					arrcode[1]=indxx;
					Reroute(arrcode);						//Pass code to rerouter to determine output
					break;
				}else{
					continue;
				}
			}
		}
	}
}


The 3-D array is used to hold a list of strings used to compare what the user types against valid commands. The FOR loop inside a loop is for the actual comparison.

When I use the debugger and type in a command, I receive this segmentation fault alert: "SIGSEGV." The Call Stack lists the top stack item at address 0x0.

While building, I tested this function with a simpler, single-level FOR loop, which worked fine.

I tried searching through forums for my problem, but I haven't found any discussions that pertain to the issue I have.
Last edited on
I would blame the typo in line 26.
Consider using easy to distinguish names as `K', `L', `M'

Also, ┬┐what's the point in making it 3D if you will search all the cells?
Last edited on
I haven't really read the program thoroughly, but what is the blank brackets in string test[][25][50] supposed to do? My guess is that you set the array boundary wrong.

To my understanding, arrays MUST be defined in size at compile time.
arrlen[0] = GAL(test); // initialize with a get array length function

A get array length function? No such thing.

blueberry wrote:
To my understanding, arrays MUST be defined in size at compile time.

They must. However that size can be specified by supplying an array initializer as done above and letting the compiler figure out what the size should be.
Last edited on
Ah... Thanks ne555. I'll try that out. The reason I use inxx is because I already have other FOR loops that use i, index, etc. And the reason why I use a 3-D array is to make it easier for me to enter in new command tests. There are different types of commands, and I don't want to lazily lump them all into a giant 1-D array. Makes it hard for me to find stuff.

Also, blueberry, that was the first thing I thought of, but when I got rid of the GAL function and simply put in a number to fill in the blank, the problem persisted.

Cire, I am using a template that the GAL function utilizes... Maybe I was wrong to call it a "function?"


Thank you again ne555. Fixed that typo, and now my program works perfectly fine.
Last edited on
Topic archived. No new replies allowed.