2010-01-29 28 views
1

出於某種原因,每當我運行this program時,它將退出permute(permutater, length, lenth);。當我註釋掉該行並且該函數甚至不運行時,這不會發生。任何幫助?程序意外退出原因不明(C++)

+3

你有可能使用調試器試過嗎? –

+0

它似乎是導致段錯誤,我認爲這與指針有關? – Galileo

回答

10

我注意到的第一件事 - 你沒有初始化索引變量hor

int permute(string permutater,int length,int lenth) 
{ 
    int hor,hor2,marker; 
    cout << length/lenth; 
    for (marker=0;marker !=(length/lenth);marker++) 
     { 
      hor2 = permutater[hor];  // <== hor is not initialized 
      permutater[hor] = permutater[hor-1]; 
      permutater[hor] = hor2; 
      hor--; 
      cout << permutater; 
     } 

} 
+0

這就是答案。任何舊垃圾都可能成爲廢物,指向任何舊的記憶。訪問該內存會造成很大的麻煩。案件結案。 –

+0

非常感謝,事後看來(一如既往),很明顯,-_-。感謝您支持我。 – Galileo

2

HOR2 =交換器[HOR];

hor的值是多少?

2

我用MSVC

error C4716: 'permute' : must return a value 
warning C4700: uninitialized local variable 'hor' used 
0

沒有得到一個機會來運行它,但得到了下面的編譯錯誤,但你有沒有注意到,你在permute(string permutater,int length,int lenth)功能缺失return

另外,請#include <string>