【问题标题】:Code Blocks run error: "Process terminated with status -1073741510". What am I doing wrong?代码块运行错误:“进程终止,状态为 -1073741510”。我究竟做错了什么?
【发布时间】:2015-04-05 21:08:16
【问题描述】:

我在 Code Blocks 中运行一个 c++ 程序,但出现错误: “进程以状态 -1073741510 终止”。 现在我发现错误发生在:

new_list = new char[number_of_symbols + 1];

问题是我无法终生弄清楚我在这里做错了什么。为了提供更多上下文,我将完整的代码放在下面。

#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;
}

运行后我得到以下输出:

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.

这意味着我之前提到的那行有问题,但我不知道为什么。有人可以告诉我我在这里做错了什么吗?

【问题讨论】:

  • Google 告诉你 0xC0000005 表示访问冲突。
  • 好的,但这仍然不能真正帮助我。我如何违反任何内存访问权限?
  • Minimal 测试用例。您还没有完成调试。

标签: c++ codeblocks


【解决方案1】:

我很惊讶你能走到这一步;当我尝试运行该程序时,它实际上挂得更早。原因如下:

你的主函数调用add_transition(0, ...),它调用add_state(0),就在你的cout &lt;&lt; "Gaat dit goed?" &lt;&lt; endl;之后。

此时,add_stateif(state &gt;= number_of_states) 内部计算结果为 true(即 if(0&gt;=0)),然后调用 new 分配 0 个 transition* 元素,这是一件坏事,可能会导致错误(C++ new int[0] -- will it allocate memory?)。无论如何,在这种情况下,您的错误不在这里。继续,你现在跳过for循环,因为条件不满足,直接转到free_action_table();

在里面,你跳过for(同样的原因)然后......你尝试delete[] action_table;

action_table 何时初始化?绝不!因此它具有随机值。如果您尝试删除它,您很可能会访问一个未保留给您的进程的地址并得到一个很好的分段错误。您显示的输出表明您超出了该范围,因此您(不)幸运地拥有一个实际上对应于程序内存空间的随机值,因此它在您第一次到达它时并没有引发分段错误。

所以要解决您的问题(尽管我不能保证它是唯一的问题),您必须在删除它之前初始化该指针,或者您必须避免在它尚未初始化时删除它。您还可以看看智能指针,它们可以处理内存管理并防止这些错误。

【讨论】:

  • 感谢您的反馈。我的程序中仍然有一些错误,同样的错误,但至少现在我真的知道要寻找什么了!
  • 我终于找到了我所有的错误!代码中实际上有很多地方导致了同样的错误。如果您没有指出我的代码中还有更多问题,我仍然会在程序停止的那一行中搜索错误。
猜你喜欢
  • 2014-06-15
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
相关资源
最近更新 更多