如何在 C++ 中保存动态数组中的值?

How can I save a value from a dynamic array in C++?

编辑:问题已解决,我正在访问由于不正确的 while() 条件而未初始化的数据。我已将其从 OR 更改为 AND。它现在按预期工作。谢谢!


我试图在 C++ 中找到两个数组之间的交集。我已经编写了可以执行我想要的操作的代码,但是当我删除 [] 数组时,它会中断,从而导致浮点异常。 (除以零?)如何保存我想要的值,而不导致我的程序内存泄漏?

如果我省略 delete[] 语句,此代码将按照我预期的方式工作,但我认为这会导致内存泄漏。如果我省略语句 biggestIntersection = *(factorsa + i),它将 return 1;我该怎么做才能保存 factora + i 的值并随后删除数组以避免内存泄漏?

const int Fraction::findgcf(int a, int b) const{
    a = std::abs(a); //absoute value
    b = std::abs(b);

    int* factorsa = new int[a]; //dynamic array of ints to store factors of a
    int* factorsb = new int[b]; //dynamic array of ints to store factors of b


    int numFactorsa = 0;
    for(int i = 1; i <= a; i++){//iterate over the ints from 1 to a
        if(a % i == 0) {//if we find a factor of a
            *(factorsa + numFactorsa) = i;// and append that to the array
            numFactorsa++;
        }

    }

    int numFactorsb = 0;
    for(int i = 1; i <= b; i++){
        if(b % i == 0){
            *(factorsb + numFactorsb) = i;
            numFactorsb++;
        }
    }

    int biggestIntersection = 1;

     int i = 0, j = 0;
    while(i < numFactorsa || j < numFactorsb){//while we are in the bounds of the two arrays
        if(*(factorsa + i) < *(factorsb + j)){ //if the factor of a is less than the factor of b
            i++;                               //move the index of a up one
        } else if (*(factorsa + i) > *(factorsb + j)){ //if the factor of b is less than the factor of a
            j++;                                       //move the index of b up one
        } else {                                    //otherwise they must be equal
            biggestIntersection = *(factorsa + i); //so that is the new biggest intersection between the sets
            i++; j++;
        }
    }

    delete [] factorsa;
    delete [] factorsb;
    return biggestIntersection;

你真的应该使用 std::vector。那就不用担心清理了。

const int Fraction::findgcf(int a, int b) const{
    a = std::abs(a); //absoute value
    b = std::abs(b);

    std::vector<int> factorsa(a);
    std::vector<int> factorsb(b);

    int numFactorsa = 0;
    for(int i = 1; i <= a; i++){//iterate over the ints from 1 to a
        if(a % i == 0) {//if we find a factor of a
            factorsa[numFactorsa] = i;// and append that to the array
            numFactorsa++;
        }

    }

    int numFactorsb = 0;
    for(int i = 1; i <= b; i++){
        if(b % i == 0){
            factorsb[numFactorsb] = i;
            numFactorsb++;
        }
    }

    int biggestIntersection = 1;

    int i = 0, j = 0;
    while(i < numFactorsa || j < numFactorsb){//while we are in the bounds of the two arrays
        if(factorsa[i] < factorsb[j]){ //if the factor of a is less than the factor of b
            i++;                               //move the index of a up one
        }
        else if (factorsa[i] > factorsb[j])
        {                                              //if the factor of b is less than the factor of a
            j++;                                       //move the index of b up one
        } else {                                    //otherwise they must be equal
            biggestIntersection = factorsa[i];      //so that is the new biggest intersection between the sets
            i++; j++;
        }
    }

    return biggestIntersection;
}

最大的问题 - 可能是导致错误的原因,尽管一个最小的例子可以使它更清楚 - 是您正在访问尚未初始化的内存。这会产生不可预测的行为。

int* factorsa = new int[a]; 不会将该数组中的每个 int 设置为零 - 数组的内容实际上可以是任何内容。稍后,在您的第一个 for 循环中,您确实设置了一些数组位置的值,但不是全部。因此在最后的 for 循环中,您无法知道要输出什么。这将取决于您要求 new 提供的内存位置的或多或少的随机内容。

(另外,作为注释,您的 while 循环条件是错误的。)