我有一個項目需要不同類型的迭代。一個是5000的char數組上的函數遞歸。經過50次調用後,它會崩潰,假設從堆棧溢出。不知道如何解決這個問題。大量函數遞歸 - C++
void functionLoop(int loopInt)
{
#ifdef ___EXSTR_H___
#undef ___EXSTR_H___
#endif
#include "exstr.h"
ofstream fout;
fout.open("output.txt");
int arrayLength = sizeof (example_strings)/4; // arrayLength = 5000.
char *stringArray = example_strings[loopInt];
int charCount = 0;
while(*stringArray != 0)
{
stringArray++;
charCount++;
}
cout << loopInt + 1 << ": " << charCount << ": " << example_strings[loopInt] << endl;
loopInt++;
if(loopInt < arrayLength)
{
functionLoop(loopInt);
}
}
編輯:
我清理代碼很多,擺脫了所有的變量,感動了頭文件參數,並獲得約4500多個迭代,但4546後仍然崩潰。這裏的更新代碼:
void functionLoop(char * example_strings[], ofstream &outputFile, int counter)
{
outputFile << counter + 1 << ": " << strlen(example_strings[counter]) << ": " << example_strings[counter] << endl;
counter++;
if(counter < ARRAY_SIZE)
{
functionLoop(example_strings, outputFile, counter);
}
}
謝謝大家的幫助。
那是什麼'#ifdef','#undef','#endif','的#include '順序?爲什麼你在函數*中包含頭文件? – 2012-02-10 06:09:18
因爲我不允許將其稱爲全球實體。還有另外3個函數調用相同的頭文件,但是沒有定義它,它會給編譯錯誤。 – Resun 2012-02-10 06:16:09
@Resun我猜你的#include「exstr.h」聲明瞭一個長度爲5000字節的example_str?如果是這樣,請將標題移出。這是你的問題的根源:) – Microkernel 2012-02-10 06:20:12