2017-05-18 35 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真的,但編譯器可以允許它作爲擴展?如果不知道編譯器或者它給出了什麼警告,就不可能更具體地說出來。 –