函数问题

时间:2017-05-18 13:53:11

标签: c++ arrays function

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 << "\n\n";

        // 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; 添加到 factorize 函数时,我得到了奇怪的结果。出于某种原因,修改三角形的值。

#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 << "\n\n";

        // find factors of triangle number
        factorize(factors, triangle);
        std::cout<<"\n";

    }

    return 0;

}

意外结果(请注意三角值如何变为: 0 1 3 ,但是某些原因会跳回 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

我错过了什么?

1 个答案:

答案 0 :(得分:4)

定义

int factors[] = {};

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

如果你想要一个动态的&#34;阵列&#34;在C ++中,您应该使用std::vector

如果您事先知道尺寸,请使用该尺码,或使用std::array