0

I was running a c++ program in Code Blocks, but I got the error: "Process terminated with status -1073741510". By now I figured out that the error occurs at:

new_list = new char[number_of_symbols + 1];

The problem is that I can't figure out for the life of me what I'm doing wrong here. To give more context I've put the complete code below.

#include <iostream>
#include <math.h>
#include <time.h>

using namespace std;

struct transition{
  int write_symbol;
  int move_head;
  int next_state;
};

class TM{
public:
  TM();
  ~TM();
  void add_transition(int, char, int, char, int);
  void remove_transition(int, char);
  void run();
  void run(char*, int);
private:
  int current_state;
  int head_position;
  int number_of_states;
  int number_of_symbols;
  char* symbol_list;
  transition** action_table;
  void add_state(int);
  int add_symbol(char);
  void print(char*);
  void free_action_table();
};

TM::TM(){
  number_of_states = 0;
  number_of_symbols = 0;
}

TM::~TM(){
  free_action_table();
}

void TM::add_transition(int p, char sigma, int q, char tau, int D){
  int symbol_id1, symbol_id2;
  cout << "Gaat dit goed?" << endl;
  add_state(p);
  cout << "Gaat dat goed?" << endl;
  add_state(q);
  cout << "En gaat deze goed?" << endl;
  symbol_id1 = add_symbol(sigma);
  cout << "Gaat die ook goed?" << endl;
  symbol_id2 = add_symbol(tau);
  cout << "Gaat misschien alles goed?" << endl;
  action_table[symbol_id1][p].write_symbol = symbol_id2;
  action_table[symbol_id1][p].next_state = q;
  action_table[symbol_id1][p].move_head = D;
  cout << "Alles gaat goed!" << endl;
}

void TM::add_state(int state){
  transition** new_table;
  if(state >= number_of_states){
    new_table = new transition*[number_of_symbols];
    for(int i = 0; i < number_of_symbols; i++){
      new_table[i] = new transition[state + 1];
      for(int j = 0; j < number_of_states; j++){
        new_table[i][j] = action_table[i][j];
      }
      for(int j = number_of_states; j <= state; j++){
        new_table[i][number_of_states - 1].write_symbol = -1;
        new_table[i][number_of_states - 1].next_state = -2;
        new_table[i][number_of_states - 1].move_head = 0;
      }
    }
    free_action_table();
    number_of_states = state + 1;
    action_table = new_table;
  }
}

int TM::add_symbol(char symbol){
  transition** new_table;
  char* new_list;
  cout << "Gaat hier iets fout?" << endl;
  for(int i = 0; i < number_of_symbols; i++){
    if(symbol_list[number_of_symbols] == symbol){
      return i;
    }
  }
  new_table = new transition*[number_of_symbols + 1];
  cout << "Gaat daar iets fout?" << endl;
  new_list = new char[number_of_symbols + 1];
  cout << "Of misschien hier?" << endl;
  for(int i = 0; i < number_of_symbols; i++){
    new_table[i] = new transition[number_of_states];
    new_list[i] = symbol_list[i];
    for(int j = 0; j < number_of_states; j++){
      new_table[i][j] = action_table[i][j];
    }
  }
  for(int j = 0; j < number_of_states; j++){
    new_table[number_of_symbols][j].write_symbol = 0;
    new_table[number_of_symbols][j].next_state = -2;
    new_table[number_of_symbols][j].move_head = 0;
  }
  cout << "Zou het kunnen?" << endl;
  new_list[number_of_symbols] = symbol;
  free_action_table();
  if(number_of_symbols > 0){
    delete[] symbol_list;
  }
  symbol_list = new_list;
  number_of_symbols++;
  action_table = new_table;
  cout << "Alles gaat goed!" << endl;
  return number_of_symbols - 1;
}

void TM::free_action_table(){
  for(int i = 0; i < number_of_symbols; i++){
    delete[] action_table[i];
  }
  delete[] action_table;
}

int main(){

  TM one = TM();
  one.add_transition(0,'0',0,'1',0);
  one.add_transition(0,'1',-1,'1',0);

  return 0;
}

After running I got the following output:

Gaat dit goed?
Gaat dat goed?
En gaat deze goed?
Gaat hier iets fout?
Gaat daar iets fout?
Of misschien hier?
Zou het kunnen?
Alles gaat goed!
Gaat die ook goed?
Gaat hier iets fout?
Gaat daar iets fout?

Process returned -1073741819 (0xC0000005)   execution time : 2.028 s
Press any key to continue.

This would imply that something is wrong with the line I mentioned before, but I can't figure out why. Can someone tell me what I'm doing wrong here?

user3635700
  • 135
  • 7

1 Answers1

0

I am surprised you get so far; when I try running the program, it actually hangs much earlier. Here's why:

Your main function calls add_transition(0, ...), which calls add_state(0), just after your cout << "Gaat dit goed?" << endl;.

At this point, inside add_state, if(state >= number_of_states) evaluates to true (it's if(0>=0)), and then you call new to allocate 0 transition* elements, which is a bad thing which could lead to errors (C++ new int[0] -- will it allocate memory?). Anyway, in this case your error isn't here. Just go on, you now skip the for loop as the condition isn't met, and go straight to free_action_table();.

Inside it, you skip the for (same reason) and then... You try to delete[] action_table;.

When was action_table initialised? Never! Therefore it has a random value. If you try to delete it, you will most probably access an address that isn't reserved to your process and get a nice segmentation fault. The output you show indicates you went beyond that, so you were (un)lucky enough to have a random value that actually corresponds to your program's memory space, therefore it didn't throw a segmentation fault the first time you reached it.

So to fix your problem (though I can't guarantee it's the only one) you have to either initialise that pointer before you delete it, or you have to avoid deleting it when it hasn't been initialised yet. You might also have a look at smart pointers, they can take care of memory management and prevent these errors.

Community
  • 1
  • 1
  • Thanks for the feedback. I still have some mistakes in my program with the same error, but atleast now I really know what to look for! – user3635700 Apr 06 '15 at 09:41
  • I finally found all my mistakes! There were actaully quite a few places in the code that caused the same error. If you hadn't pointed out that there were more problems in my code, I would still be searching for an error in that one line at which the program stopped. – user3635700 Apr 06 '15 at 15:03