【问题标题】:Runtime error: _block_type_is_valid(phead- nblockuse)运行时错误:_block_type_is_valid(phead-nblockuse)
【发布时间】:2013-07-29 05:10:33
【问题描述】:

首先,我将向您展示我的代码。

std::ifstream file("accounts/22816.txt");
if(file){
   char *str[50];
   int count=0;
   str[0] = new char[50];
   while(file.getline(str[count], 50)){
      count++;
      str[count] = new char[50];
   }
   for(int i=0;i<count;i++){
      std::cout << str[i] << std::endl;
   }
   delete[] str;  // Here is the problem
}

前面代码的行为是:

  • 逐行读取文本文件内容。
  • 将每一行保存在二维数组的项中。
  • 打印二维数组的项目。
  • 最后从内存中删除数组and this reason of the problem.

测试我的应用程序时总是给我运行时错误消息_block_type_is_valid(phead- nblockuse).

我知道这个问题是因为delete[] str;

【问题讨论】:

    标签: c++ dynamic-variables


    【解决方案1】:

    str 是一个指针数组,每个指针都指向一个动态分配的数组。

    您需要遍历它并在每个元素上调用delete []

    for(int i=0; i < count; ++i){
      delete [] str[i];
    }
    

    注意:我已经给 OP 提供了an example using std::vector, std::string and std::getline

    【讨论】:

      猜你喜欢
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 2015-02-11
      • 2015-09-22
      • 2013-11-10
      相关资源
      最近更新 更多