如何解决C6386警告?
How to solve C6386 warning?
我正在编写一个简单的代码来从 .txt 文件中读取系统化数据,并收到警告 "C6386: Buffer overrun while writing to 'points': the writable size is 'num*8' bytes, but '16' bytes might be written"。我的情况如何解决?附上代码。
struct point {
int x, y;
};
void main()
{
fstream file;
point* points;
int num,
i = 0;
file.open("C:\Users\Den\Desktop\file.txt", fstream::in);
if (!file.is_open()) {
cout << "No file found\n";
exit(1);
}
else {
file >> num;
points = new point[num];
}
while (file >> num) {
points[i].x = num; // <- here
file >> num;
points[i].y = num;
i++;
}
file.close();
}
这只是一个警告,但它给出了很好的建议。如果文件包含超过 num
项怎么办?警告告诉你应该确保你不写超过数组的末尾。具体来说:
此警告表示指定缓冲区的可写范围可能小于用于写入它的索引。这会导致缓冲区溢出。 [msdn]
此代码不产生警告 (VS2019):
int x, y;
while (i < num && (file >> x >> y)) {
points[i].x = x;
points[i].y = y;
i++;
}
还有更多错误检查要添加。如果 file >> num;
失败怎么办?如果 num
是负数怎么办?如果 points = new point[num];
失败怎么办 (returns nullptr
)?
更新了完整的错误检查:
struct point {
int x, y;
};
void main()
{
ifstream file("C:\Users\Den\Desktop\file.txt");
if (!file) {
cerr << "No file found\n";
exit(-1);
}
int num;
if (!(file >> num) || num <= 0) {
cerr << "invalid num\n";
exit(-1);
}
point *points = new point[num];
if (!points) {
cerr << "new failed\n";
exit(-1);
}
int num_items = 0;
while (num_items < num && file >> points[num_items].x >> points[num_items].y) {
num_items++;
}
// Do some work here
delete [] points;
}
将来,考虑在原始数组上使用 std::vector
。
我正在编写一个简单的代码来从 .txt 文件中读取系统化数据,并收到警告 "C6386: Buffer overrun while writing to 'points': the writable size is 'num*8' bytes, but '16' bytes might be written"。我的情况如何解决?附上代码。
struct point {
int x, y;
};
void main()
{
fstream file;
point* points;
int num,
i = 0;
file.open("C:\Users\Den\Desktop\file.txt", fstream::in);
if (!file.is_open()) {
cout << "No file found\n";
exit(1);
}
else {
file >> num;
points = new point[num];
}
while (file >> num) {
points[i].x = num; // <- here
file >> num;
points[i].y = num;
i++;
}
file.close();
}
这只是一个警告,但它给出了很好的建议。如果文件包含超过 num
项怎么办?警告告诉你应该确保你不写超过数组的末尾。具体来说:
此警告表示指定缓冲区的可写范围可能小于用于写入它的索引。这会导致缓冲区溢出。 [msdn]
此代码不产生警告 (VS2019):
int x, y;
while (i < num && (file >> x >> y)) {
points[i].x = x;
points[i].y = y;
i++;
}
还有更多错误检查要添加。如果 file >> num;
失败怎么办?如果 num
是负数怎么办?如果 points = new point[num];
失败怎么办 (returns nullptr
)?
更新了完整的错误检查:
struct point {
int x, y;
};
void main()
{
ifstream file("C:\Users\Den\Desktop\file.txt");
if (!file) {
cerr << "No file found\n";
exit(-1);
}
int num;
if (!(file >> num) || num <= 0) {
cerr << "invalid num\n";
exit(-1);
}
point *points = new point[num];
if (!points) {
cerr << "new failed\n";
exit(-1);
}
int num_items = 0;
while (num_items < num && file >> points[num_items].x >> points[num_items].y) {
num_items++;
}
// Do some work here
delete [] points;
}
将来,考虑在原始数组上使用 std::vector
。