初始化二维动态数组c ++时读取访问冲突

方12

我正在为一个类做一个项目(已经提交),但是当我初始化 int 2D 动态数组时读取访问冲突仍然困扰着我,我不知道是什么导致了它。

class kMeans
{
public:
    //xyCoord struct
    struct xyCoord
    {
        int Label;
        int xCoordinate;
        int yCoordinate;
    };

    //variables
    int K;
    xyCoord *Kcentroids;
    int numPts;
    aPoint *pointSet;
    int numRow;
    int numCol;
    int **imageArray = NULL;
    int changeLabel;

    //constructor
    kMeans(int clusterNum, int numPoints, int row, int col)
    {
        //initializes the row and column values
        numCol = col;
        numRow = row;

        //Allocate the row and column as the size of the 2D array
        imageArray = new int*[row];
        for (int i = 0; i < row; i++)
        {
            imageArray[i] = new int[col];
        }

        //initializes the 2D array to contain all 0s
        for (int i = 0; i < row - 1; i++)
        {
            for (int j = 0; i < col - 1; j++)
            {
                imageArray[i][j] = 0;   //read access violation occurs here
            }
        }

        //Allocate numPoints as the size of the array
        pointSet = new aPoint[numPoints];
        numPts = numPoints;

        //Allocate clusterNum as the size of the array
        Kcentroids = new xyCoord[clusterNum];
        K = clusterNum;

        //Initialize the labels for each Kcenteroid
        for (int i = 0; i < K; i++)
        {
            Kcentroids[i].Label = i + 1;
        }
    }

之前没有出现过错误,但是当我决定在提交之前再次运行程序时,出现了读取访问冲突,所以我不确定是什么原因造成的。

萨马拉斯

改变这个:

for (int j = 0; i < col - 1; j++)

对此:

for (int j = 0; j < col - 1; j++)

因为您要检查 的条件j,而不是i

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在C ++中使用memset或fill_n初始化动态二维数组

C ++:读取Lambda捕获的指针时发生访问冲突

读取C中的字符串时发生访问冲突错误

矢量的C ++读取访问冲突

在VS2019中使用GTK +时,ffiw64.c中的读取访问冲突

C ++中具有动态二维数组的访问冲突

错误:在C中初始化二维结构变量时的预期表达式

从C中的二进制文件读取字符串时发生访问冲突

c-读取数组时访问冲突

从文件读取时发生C ++访问冲突

在Java中使用动态大小初始化二维字符串数组

删除二维数组时发生访问冲突

尝试使用成员函数访问动态分配的成员变量时发生读取访问冲突

C ++-读取访问冲突

在bash中初始化二维数组时出错

C++ Int 似乎没有初始化并抛出异常:读取访问冲突

如何初始化二维数组以及如何从java中的main方法访问它?

在 C 中动态初始化数组时出错

无法解决动态初始化二维数组的问题

初始化二维数组

C中的动态二维数组不读取第一个元素

C++ 中二维数组的声明冲突?

使用 __func__ 标识符时 C++ 访问冲突读取位置 0xFFFFFFFFFFFFFFFF

c中的二维动态数组 - 访问分配的变量

从文件写入字符串数组的指针时出错(读取访问冲突)?CPP

当我想用它的地址初始化一个二维字符数组时出现段错误

C访问一维数组作为二维数组

在静态结构实例中初始化和访问二维字符数组

二维动态数组指针访问