Program stops running on simple array assignment

Hello,

This has been driving me crazy all afternoon. Program runs great until I add a simple two dimensional array assignment to this function (transTable[i][j] = k;) and then program executes but hangs. I have checked to ensure that the values of i,j,and are valid and well within range ( current transTable is configured to be transTable[100][500] and i, j, and k never are larger than 10 )...

Any ideas would sure be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  void TransitionTable(void) {
    int i, k, j=0; // index variables
    char linebuffer[256]; // line buffer to temp store line input from file
    
    file.getline(linebuffer, '\n');  // fetch next line from file into buffer
    while (linebuffer[0] != '#') {    // # is end of state machine delimiter
      // std::cout << "\n      " << linebuffer[1] << " " << linebuffer[3] << " " << linebuffer[5];   
        
        i = linebuffer[1] - '0'; // convert current state to integer
        k = linebuffer[5] - '0';  // convert next state to integer
        while((alphabet[j]) != (linebuffer[3]) ) j++; //find index of alphabet[]
        
       //  std::cout << " " << i << " " << alphabet[j] << " " << k;
       
        /**********  this is the simple assignment hanging the program *****/
        transTable[i][j] = k; // enter next step in transition table

        file.getline(linebuffer, '\n');  // get next line of file
    }
    return;
}
Last edited on
Hi,


Are the variables transTable , alphabet and file global? If not they are out of scope.

Have you tried a debugger? That will show the problem straight away.

Thank you for your quick response. Yes, transTable, alphabet, and file are all global.

I am just learning netBeans and C++ so not familiar with the debugger but am using the "poor man's debugger' approach, i.e. std::cout all of the variables and I can see that i, j, and k and exactly what they should be which is what is driving me crazy.

I can't think of anything that would cause the program to hang as I am not writing outside the bounds of the two dimensional array...
Ok, can you tell what the value of linebuffer is?

I can then have a go a hard coding an example on my compiler.

Some things:

Do you have all the warnings turned on for your compiler? With g++ I use
-Wall -Wextra -pedantic


What is your compiler?

Using global variables is generally a bad idea, unless you really know what you are doing.

Always initialise your variables when you declare them. With i and k you set them soon after but it is still asking for trouble one day.

I see you are using std::cout, so it is C++ as opposed to C: Any particular reason why you aren't using STL containers & algorithms? They make life a lot easier. Sometimes novices at C++ do a mixed C/ C++ approach (I did too, at the start)

For example have a look at std::string and it's member functions & algorithms.

Does netbeans have a debugger as part of the IDE? IF so it should be fairly easy to use.
Thanks,

Program is relatively simple and I don't want to cluttup the function arguments so that is why I have them global for now...

Thanks for tips re: initializing variables.

will looking into std::string but I have most of this program written at this point..

linebuffer[] is the following character string at beginning of first iteration:

"0 o 0" ( o is alphabet[0] )...

using g++ in backend of netBeans.

I have been playing around with the netBeans debugger but it is telling me transTable is out of scope which it is not because it is global...
Ok, How big is your program? If it doesn't fit into 1 or 2 posts, then post it on coliru-stacked say

http://coliru.stacked-crooked.com


linebuffer[] is the following character string at beginning of first iteration:

"0 o 0" ( o is alphabet[0] )...

Just noticed, linebuffer[1] is a space char (ascii value 32), so i = linebuffer[1] - '0' is -16 because 0 is ascii 48, hence out of bounds access. Why didn't that show up in your output?

How is the 'o' alphabet[0] ?

Edit: I see form line 11, but arrays start a t 0, so linebuffer[3] is 'space'

Also linebuffer[5] is garbage because you only assigned 5 chars to it, the last one is linebuffer[4], array start at zero.

To avoid problems like this, don't hard code numbers into your code. Obtain the length of the string and make use of that variable.

...... but it is telling me transTable is out of scope which it is not because it is global...


If it is telling you it is out of scope, then it is.

using g++ in backend of netBeans.


great, you can use the same warnings as me. I also have a clang++ compiler, it gives easy to read warnings and has basically the same warnings as gcc
Last edited on
deleted.


Last edited on
deleted.

Last edited on
Ok geat, can you please format the code in your post with code tags, select the code then press the <> button under the format menu on the right.

I will have a go at debugging your code.
Thank you - really appreciate it!
Hi, I got this output - hopefully meaningful for you

Numstates 4
Finite State Automaton #1
(1) number of states: 4
(2) final states: 0123
(3) alphabet: ocUD
(4) transistions:
 0 o 0 0 o 0
 0 c 1 0 c 1
 1 c 1 1 c 1
 1 U 2 1 U 2
*** Program received signal SIGSEGV (Segmentation fault) ***



I added this code at the start: One should always check if file opening works

1
2
3
4
5
6
7
if (file.is_open() ) {
		setupMachine(); // set up Machine
		// processMachine(); // process Machine
	}
	else {
		std::cout << "File Not open\n";
	}


I had it segfaulting here, with j at 202176
while((alphabet[j]) != (linebuffer[3]) ) j++; //find index of alphabet[]

j was 2 and linebuffer[3] was 'o'

I guess it didn't find an 'o', and became an endless loop.

Hope that helps a bit.
Thanks - appreciate your help.

Will check into it closer but as 'o' is actually the first element of alphabet it should always find it..

At least we know it was not an out of bounds error on the array but still scratching my head why it would not find the first element of alphabet[]...

I even print out alphabet in item (3) of the output so it is definitely there...
Thanks again - realized I made a really stupid error - never set j back to 0!!!

I was stuck on the program hanging when I uncommented out the transTable[][] array!

You made my night - thanks!
No worries - another happy customer!!

I really urge you to learn how to use the debugger - it will save you days of fooling around wondering why things don't work.

Also, get rid of your global variables, get used to sending arguments to functions. My debugger doesn't show global variables

Some other minor things:

there is no need for return statements in void functions, unless you want to return early.

void in an argument list has never been a requirement for C++, it was in the early days of C.

don't use #defines for variables, make them const variables instead:

const unsigned int SIZE = 10;

As I said earlier, don't have magic numbers like 256 in your code, make them const variables as well.

Good Luck !
I forgot to mention, The STL has member functions like find, find_first_of etc, plus untold algorithms.

Save you having to write code to do that and risk getting errors in doing so - just makes life a lot easier.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/algorithm/
Thanks- really appreciate it.

I am just getting started with C++ and you helped me quite a bit.

I will look into STL when I get more time and incorporate your other suggestions immediately.

Thanks again!
Topic archived. No new replies allowed.