c ++删除用new分配的char指针

Alo*_*123 4 c++ char new-operator dynamic-memory-allocation delete-operator

在这段代码中,当第一个数字是二维数组的大小时,我从文件中获取数字。

在我的代码中,我定义

char *filename=new char;
Run Code Online (Sandbox Code Playgroud)

(我必须使用 char *filename,这是练习..)一切正常,直到我尝试使用delete. 双方deletedelete[]给了我错误和崩溃我的程序。

这是我的完整代码:

#include <iostream>
#include <fstream>
using namespace std;
double **readmat(char *filename, int *size)/////question 2
{
    ifstream read(filename);
    cout << filename << endl;
    if (!read)
    {
        cout << "Can't open file!" << endl;
        exit(1);
    }
    read >> *size;
    double **mat = new double*[*size];
    for (int i = 0; i < *size; i++)
    {
        mat[i] = new double[*size];
        for (int j = 0; j < *size; j++)
        {
            read >> mat[i][j];
        }
    }    
    read.close();    
    return mat;
}
int main()
{
    int size;
    char *filename = new char;
    filename = "text.txt"; 

    double **arr = readmat(filename, &size);
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            cout << arr[i][j]<<"  ,  ";
        }
        cout << endl;
    }
    cout << endl;

    delete filename; //<-------- this crashed my code
    for (int i = 0; i < size; i++)
    {
        delete[] arr[i];
    }
    delete[] arr;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我的文件的外观:

在此处输入图片说明

这是运行代码后控制台应用程序的样子:

在此处输入图片说明

这是我期望得到的,但我收到此错误:

在此处输入图片说明

有谁知道这会发生什么,我能做些什么来解决它?

Rem*_*eau 5

您正在尝试delete一个char*未在与分配的内存指向new

在这一行:

char *filename = new char;
Run Code Online (Sandbox Code Playgroud)

你做了new一些记忆(一个单一的char,而不是一串chars)。但随后在这一行:

filename = "text.txt"; 
Run Code Online (Sandbox Code Playgroud)

您将char*指针更改为指向完全不同的内存,从而泄漏您使用的内存new

然后在这一行:

delete filename;
Run Code Online (Sandbox Code Playgroud)

您尝试delete"text.txt"文字,而不是charnew“版。这就是你崩溃的原因。

对于您尝试做的事情,您需要这样做:

delete filename;
Run Code Online (Sandbox Code Playgroud)

但是,您根本不应该使用new/ new[]for filename。使用std::string来代替:

char *filename = new char[strlen("text.txt")+1];
strcpy(filename, "text.txt");
...
delete[] filename;
Run Code Online (Sandbox Code Playgroud)

或者:

#include <fstream>
#include <string>

double **readmat(const std::string &filename, int *size)
{
    std::ifstream read(filename.c_str());
    ...
}

int main()
{
    int size;
    double **arr = readmat("text.txt", &size);
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后,当你在做的时候,你也不应该使用new[]你的矩阵。使用std::vector来代替:

#include <vector>

std::vector< std::vector<double> > readmat(char *filename)
{
    ...

    int size;
    read >> size;

    std::vector< std::vector<double> > mat(size);
    for (int i = 0; i < size; i++)
    {
        mat[i].resize(size);
        for (int j = 0; j < size; j++)
        {
            read >> mat[i][j];
        }
    }    

    return mat;
}

int main()
{
    ...

    std::vector< std::vector<double> > arr = readmat("text.txt");
    size_t size = arr.size();

    for (size_t i = 0; i < size; i++)
    {
        for (size_t j = 0; j < size; j++)
        {
            std::cout << arr[i][j] << "  ,  ";
        }
        std::cout << endl;
    }
    std::cout << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)