2017-05-18 36 views
0

这里是C++新手,我遇到了一个我找不到的问题。函数中的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 

但是,当我添加行因素[索引] = 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 << "\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: 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 

我错过了什么?

回答

4

定义

int factors[] = {}; 

限定factors是一个阵列int。它的每个索引都将超出范围并导致未定义的行为。第一个程序的作用是因为你实际上没有使用factors

如果你想在C++中使用动态“数组”,你应该使用std::vector

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

+1

C++不支持0大小的数组。尽管如此,许多编译器提供了它作为扩展。 –

+0

@FrançoisAndrieux真的,但编译器可以允许它作为扩展?如果不知道编译器或者它给出了什么警告,就不可能更具体地说出来。 –