【问题标题】:Crash in a C++ tutorial program (reading matrix)C++ 教程程序崩溃(阅读矩阵)
【发布时间】:2015-04-02 15:00:00
【问题描述】:

我是使用 C++ 和 Microsoft Visual Studio 的新手,我尝试将数据文件(2 列 x 500 行,由浮点数组成)转换为数组,然后尝试在屏幕上输出数组。每当我尝试运行它时,它都会出现“file.exe 中 0x001BC864 处的未处理异常:0xC0000005:访问冲突写入位置 0x00320A20。”
我找到了这个视频并尝试修改该代码https://www.youtube.com/watch?v=4nz6rPzVm70 任何帮助将不胜感激。

#include<stdio.h>
#include<string>
#include<iostream> // 
#include<fstream> // 
#include<array> // 
#include<iomanip> //
#include<sstream>//
#include<conio.h>
using namespace std;

int rowA = 0; // 
int colA = 0;


int main()
{
string lineA; 
int x;
float arrayA[2][500] = { { 0 } }; 
ifstream myfile("C:/results.dat"); 
if (myfile.fail()) {
    cerr << "File you are trying to access cannot be found or opened";
    exit(1);
}

    while (myfile.good()) { 
    while (getline(myfile, lineA)) {
        istringstream streamA(lineA);
        colA = 0; 
        while (streamA >> x) { 
            arrayA[rowA][colA] = x; 
            colA++;             }
        rowA++;         }
}


cout << "# of Rows --->" << rowA << endl; 
cout << "# of Columns --->" << colA << endl; 
cout << " " << endl;
for (int i = 0; i < rowA; i++) {
    for (int j = 0; j < colA; j++) {
        cout << left << setw(6) << arrayA[i][j] << " ";
    }
    cout << endl;
}

return 0;
_getch();
 }

【问题讨论】:

  • 如果你有 500 行,每行两个浮点数,那么 float arrayA[2][500] 和 arrayA[rowA][colA] 不能很好地结合在一起。如果你有 2 行 500 个浮点数,他们会这样做。
  • 好的,谢谢,请问您有什么建议吗?
  • 这个问题不是 Visual Studio 特有的。这是一个通用的 C++ 问题。您在使用 c++ 数组时遇到问题就是问题所在。掌握编程的第一步是能够确定问题的根源。
  • float arrayA[500][2] 可能会解决此特定文件的问题。更一般地,看一下std::vector,其中元素的数量不必在编译时知道(并且可以在运行时调整),并且知道你可以有一个std::vector&lt;std::vector&lt;float&gt;&gt;。在不了解您正在阅读的数据的含义和结构的情况下,我可以提供尽可能多的建议。

标签: c++ visual-studio


【解决方案1】:

显然,您对数组的访问超出了范围,因为您的数组索引超出了数组维度的大小。

鉴于这不会是您最后一次遇到此类问题,我的回答将为您提供有关如何检测此类问题是否出错的提示。

您的第一个工具是向您的代码添加断言,以使问题在您的代码的调试版本中变得明显。

 #include <cassert>

 // ...
 while (myfile.good()) { 
 while (getline(myfile, lineA)) {
    istringstream streamA(lineA);
    colA = 0; 
    while (streamA >> x) { 
        // probably you would have noticed the error below while writing 
        // those assertions as obviously you would notice that you want 500
        // rows and not 2 and you want 2 columns, not 500...
        assert( rowA < 2 ); // <<--- This will assert!
        assert( colA < 500 ); // <<--- This will assert if there are more than 500 lines in the file.
        arrayA[rowA][colA] = x; 
        colA++;             }
    rowA++;         }

仅使用这 2 行(和包含),您就可以看到代码出现问题的地方。

在这种情况下,修复您的代码非常容易,我将其留给您作为练习 ;)

为了避免混淆多维数组的索引,您可以以更具暗示性的方式编写代码(更具可读性)。

int main()
{
    string lineA; 
    int x;
    const int NUM_COLUMNS = 2;
    const int NUM_ROWS = 500;
    float arrayA[NUM_COLUMNS][NUM_ROWS] = { { 0 } }; 
    // ...

这一点额外的表现力增加了您发现下面的数组访问使用错误的每个数组维度的索引变量的几率。

最后一点,如果输入文件不违反您的假设(2 列,少于 501 行),您应该添加额外的检查,因为您的程序只能正常工作(修复后)。这属于“防御性编程”一章 - 即您的代码可以保护自己免受超出其控制范围的假设的违反。

顺便说一句,您在底部的打印循环中重复了您的错误。在那里,您也可以添加断言。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多