0

运行以下代码时出现的错误是“Debug Assertation Failed... Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)”。

我的 readTXT 方法需要传递一个 char* 对象,但我想允许用户选择为自己输入值。

char * mapName;
int main()
    {
        //load map
        int mapSelection;
        cout << "select a map";
        cin >> mapSelection;

        switch (mapSelection)
        {
        case 1:
            mapName = "walls1.txt";
            break;
        case 2:
           mapName = "walls2.txt";
           break;
        case 3:
            mapName = "maze1.txt";
            break;
        case 4:
           mapName = "maze2.txt";
           break;
        }

        map = readTXT(mapName, 8, 11);
        delete mapName;
    ...

这是 readTXT 方法的代码

double* readTXT(char *fileName, int sizeR, int sizeC)
{
  double* data = new double[sizeR*sizeC];
  int i=0;
  ifstream myfile (fileName);
  if (myfile.is_open())
  {

    while ( myfile.good())
    {
       if (i>sizeR*sizeC-1) break;
         myfile >> *(data+i);
         cout << *(data+i) << ' '; // This line display the converted data on the screen, you may comment it out. 
         if (i == 10 || i == 21 || i == 32 || i == 43 || i == 54 || i == 65 || i == 76)
         {
             cout << "\n";
         }
         i++;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
  //cout << i;

  return data;
}
4

2 回答 2

1

你不应该这样做:

delete mapName;

因为mapName来自一个字符串常量。只有delete您分配的内存new

字符串常量内置在您的程序中,不需要删除。当您使用 a 引用一个时char*,您不是在制作副本,因此不会引入任何删除的需要。

于 2013-04-04T18:07:12.127 回答
1

不要删除你没有得到的内存new

delete mapName;

删除上面的行。

于 2013-04-04T18:07:15.990 回答