[英]c++ deleting char pointer allocated with new
In this code I am getting numbers from a file, when the first number is the size of the 2D array.在此代码中,当第一个数字是二维数组的大小时,我从文件中获取数字。
In my code I'm defining在我的代码中,我正在定义
char *filename=new char;
(I have to use char *filename, this is the exercise..) Everything works fine, until the moment I try to delete
. (我必须使用 char *filename,这是练习..)一切正常,直到我尝试delete
. both delete
and delete[]
gives me error and crashing my program. delete
和delete[]
都给我错误并使我的程序崩溃。
This is my full code:这是我的完整代码:
#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;
}
This is how my file looks:这是我的文件的样子:
This is what the console app looks like after running the code:这是运行代码后控制台应用程序的样子:
Which is what I am expecting to get, but I get this error:这是我期望得到的,但我收到此错误:
Does anyone have any idea what could this happen, and what I can do to fix it?有谁知道这会发生什么,我能做些什么来解决它?
You are trying to delete
a char*
that is not pointing at memory allocated with new
.您正在尝试delete
未指向分配给new
的内存的char*
。
On this line:在这条线上:
char *filename = new char;
You do new
some memory (a single char
, not a string of char
s).你做了一些new
的记忆(单个char
,而不是一串char
)。 But then on this line:但是然后在这条线上:
filename = "text.txt";
You change the char*
pointer to point at completely different memory, thus leaking the memory you new
'ed.您更改char*
指针以指向完全不同的内存,从而泄漏您new
的内存。
Then on this line:然后在这一行:
delete filename;
You try to delete
the "text.txt"
literal, not the char
you new
'ed.您尝试delete
"text.txt"
文字,而不是您new
的char
。 That is why you crash.这就是你崩溃的原因。
For what you are attempting to do, you need to do this instead:对于您尝试执行的操作,您需要改为执行以下操作:
char *filename = new char[strlen("text.txt")+1];
strcpy(filename, "text.txt");
...
delete[] filename;
However, you really should not be using new
/ new[]
for filename
at all.但是,您真的根本不应该使用new
/ new[]
作为filename
。 Use std::string
instead:使用std::string
代替:
#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);
...
}
Alternatively:或者:
#include <fstream>
#include <string>
double **readmat(const char *filename, int *size)
{
ifstream read(filename);
...
}
int main()
{
int size;
std::string filename = "text.txt";
double **arr = readmat(filename.c_str(), &size);
// or simply:
// double **arr = readmat("text.txt", &size);
...
}
And then, while you are at it, you should not be using new[]
for your matrix, either.然后,当你这样做时,你也不应该为你的矩阵使用new[]
。 Use std::vector
instead:使用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;
}
char *filename = new char;
filename = "text.txt";
This creates a new char, then leaks it because the pointer filename
is reassigned to something that is statically declared.这将创建一个新的 char,然后将其泄漏,因为指针filename
被重新分配给静态声明的内容。
Therefore, later on you delete something else than the original char.因此,稍后您会删除原始字符之外的其他内容。
Multiple issues here (using new instead of new[], etc).这里有多个问题(使用 new 而不是 new[],等等)。 Suggestion, forget everything and use std::string and STL.建议,忘记一切,使用 std::string 和 STL。
This is the source of your problem:这是您问题的根源:
char *filename = new char;
filename = "text.txt";
filename
no longer points to dynamically allocated memory, thus you can't delete
it (and you're also leaking 1 byte of memory). filename
不再指向动态分配的内存,因此您无法delete
它(并且您还泄漏了 1 个字节的内存)。 Change your declaration to const char *filename = "test.txt";
将您的声明更改为const char *filename = "test.txt";
and remove the delete filename;
并删除delete filename;
. .
new char
allocates a single character on the heap. new char
在堆上分配一个字符。 Most functions that take a const char*
as parameter expect a pointer to the first element of an array with the null character ( \0
) as delimiter (a C-style string).大多数采用const char*
作为参数的函数都期望指向数组第一个元素的指针,其中空字符 ( \0
) 作为分隔符(C 风格字符串)。
You shouldn't even be able to assign a string literal to a variable of type char *
, at least not in standard C++.您甚至不应该能够将字符串文字分配给类型为char *
的变量,至少在标准 C++ 中是这样。 You also don't need to dynamically allocate memory for string literals, simply use您也不需要为字符串文字动态分配内存,只需使用
const char *filename = "text.txt";
Then you also don't delete pointers to string literals.然后你也不要删除指向字符串文字的指针。 (That's what causes the error most likely, you deleted a pointer that pointed to a string literal) (这很可能是导致错误的原因,您删除了指向字符串文字的指针)
Just replace只需更换
char* filename = new char;
with和
const char* filename = "text.txt";
and remove并删除
delete filename;
This is how your final code will look这就是您的最终代码的外观
int main()
{
int size;
const 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;
for (int i = 0; i < size; i++)
{
delete[] arr[i];
}
delete[] arr;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.