C++函数问题中的数组分配

C++ Array Assignment in Function Issue

本文关键字:数组 分配 函数 问题 C++      更新时间:2023-10-16

C++这里的新手,我遇到了一个我无法弄清楚的问题。

当我运行下面的代码时,我得到了我期望的正确结果:

#include <iostream>
#include <math.h>
void factorize(int *factors, int number)
{
    // declare vars
    int index = 0;
    // find factors of number
    for (int x = 1; x <= number; ++x) {
        // if x is a factor of number, save it to array
        if (number % x == 0) {
            std::cout << "   Index: " << index << ",  F: " << x << "n";
            index++;
        }
    }
}
int main()
{
    //declare vars
    int triangle = 0;
    int factors[] = {};
    //loop through all numbers
    for (int x = 1; x <= 5; ++x) {
        // calculate triangle value
        std::cout << "initial triangle value: " << triangle << ", ";
        triangle = triangle + x;
        std::cout << "x value: " << x << " , new triangle value: " << triangle << "nn";
        // find factors of triangle number
        factorize(factors, triangle);
        std::cout<<"n";
    }
    return 0;
}

正确结果:

initial triangle value: 0, x value: 1 , new triangle value: 1
   Index: 0,  F: 1
initial triangle value: 1, x value: 2 , new triangle value: 3
   Index: 0,  F: 1
   Index: 1,  F: 3
initial triangle value: 3, x value: 3 , new triangle value: 6
   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 3
   Index: 3,  F: 6
initial triangle value: 6, x value: 4 , new triangle value: 10
   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 5
   Index: 3,  F: 10
initial triangle value: 10, x value: 5 , new triangle value: 15
   Index: 0,  F: 1
   Index: 1,  F: 3
   Index: 2,  F: 5
   Index: 3,  F: 15

但是当我将线因子[index] = x;添加到因式分解函数中时,我得到了奇怪的结果。出于某种原因,主三角的值被修改了。

#include <iostream>
#include <math.h>
void factorize(int *factors, int number)
{
    // declare vars
    int index = 0;
    // find factors of number
    for (int x = 1; x <= number; ++x) {
        // if x is a factor of number, save it to array
        if (number % x == 0) {
            std::cout << "   Index: " << index << ",  F: " << x << "n";
            factors[index] = x;
            index++;
        }
    }
}
int main()
{
    //declare vars
    int triangle = 0;
    int factors[] = {};
    //loop through all numbers
    for (int x = 1; x <= 5; ++x) {
        // calculate triangle value
        std::cout << "initial triangle value: " << triangle << ", ";
        triangle = triangle + x;
        std::cout << "x value: " << x << " , new triangle value: " << triangle << "nn";
        // find factors of triangle number
        factorize(factors, triangle);
        std::cout<<"n";
    }
    return 0;
}

意外结果(注意三角形值是如何变化的:013,但由于某种原因跳回 2(:

initial triangle value: 0, x value: 1 , new triangle value: 1
   Index: 0,  F: 1
initial triangle value: 1, x value: 2 , new triangle value: 3
   Index: 0,  F: 1
   Index: 1,  F: 3
initial triangle value: 3, x value: 3 , new triangle value: 6
   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 3
   Index: 3,  F: 6
initial triangle value: 2, x value: 4 , new triangle value: 6
   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 3
   Index: 3,  F: 6
initial triangle value: 2, x value: 5 , new triangle value: 7
   Index: 0,  F: 1
   Index: 1,  F: 7

我错过了什么?

定义

int factors[] = {};

factors定义为空的int数组。它的每次索引都将超出界限并导致未定义的行为。第一个程序有效是因为您实际上没有使用factors

如果你想在C++中使用一个动态"数组",你应该使用 std::vector .

如果您事先知道大小,请使用该大小或使用 std::array .