矩阵大小总是返回8

Matrix size returns always 8

本文关键字:返回      更新时间:2023-10-16

在下面的代码中,我想找到2d数组(矩阵)的大小,但是,当我尝试这样做时,即使我在测试它之前声明了矩阵的大小,它总是输出1。而factorMatrix和factorMatrix[0]的大小看起来都是8。知道是什么引起的吗?谢谢。

factorMatrix = new int* [3];
    for(i=0; i<3; i++)
    {
        /**/
        factorMatrix[i] = new int [1];
    }   
factorCountMatrix = new int* [3];
    for(i=0; i<3; i++)
    {
        /**/
        factorCountMatrix[i] = new int [1];
    }
factorMatrix[0][0] = 2;
factorCountMatrix[0][0]= 0;
factorMatrix[1][0] = 3;
factorCountMatrix[1][0]= 0;
factorMatrix[2][0] = 5;
factorCountMatrix[2][0]= 0;
//test = checkFactorMatrix(3);
test = (sizeof(factorCountMatrix) / sizeof(factorCountMatrix[0]));
cout <<  test << endl << endl;

当你在指针上使用sizeof运算符时,你得到的是指针的大小,而不是它指向的是什么。您需要跟踪您自己分配的内存大小,或者使用其他东西,如std::arraystd::vector(我推荐)。