2012-02-10 109 views
0

我有一個項目需要不同類型的迭代。一個是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);  
    } 
} 

謝謝大家的幫助。

+1

那是什麼'#ifdef','#undef','#endif','的#include '順序?爲什麼你在函數*中包含頭文件? – 2012-02-10 06:09:18

+0

因爲我不允許將其稱爲全球實體。還有另外3個函數調用相同的頭文件,但是沒有定義它,它會給編譯錯誤。 – Resun 2012-02-10 06:16:09

+0

@Resun我猜你的#include「exstr.h」聲明瞭一個長度爲5000字節的example_str?如果是這樣,請將標題移出。這是你的問題的根源:) – Microkernel 2012-02-10 06:20:12

回答

0

傳遞數組作爲參數傳遞給你的函數,即

printLoop(outputFile, example_strings, current_index, max_index); 
+0

杜,現在我覺得自己像個白癡。謝謝。 – Resun 2012-02-10 08:18:54

0

如果是bc,則由於遞歸導致堆棧用完,請刪除遞歸調用並使用迭代方法。它很容易改變你的代碼。讓while/for循環loopInt並檢查數組[loopInt]處字符串的長度。

提示
可以使用strlen找到一個字符串的長度,你不需要手動與循環做到這一點:當(*字符串數組!= 0)

僞代碼:

i=loopInt 
while i<arrayLength { 
    print strlen(exampleString[i], exampleString[i]) 
} 

請修改我的帖子。即時通訊平板電腦上。

+0

我必須使用遞歸,基本上它是一個程序,用於while,do-while和函數遞歸迭代。使用不同的迭代做同樣的問題。我完成了所有其他的工作,但試圖如何運行遞歸是堆棧溢出造成的一個痛苦,至於長度,這就是我必須要做的。 – Resun 2012-02-10 06:12:22

+0

@Resun將你的問題標記爲家庭作業,並使用strlen而不是循環 – Adrian 2012-02-10 06:18:12

0

嘗試:

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. 
    while (loopInt < arrayLength) 
    { 
     char *stringArray = example_strings[loopInt]; 
     int charCount = strlen(stringArray); 
     stringArray += charCount; 
     cout << loopInt + 1 << ": " << charCount << ": " << example_strings[loopInt] << endl; 
     loopInt++; 
    } 
} 
1

這裏有問題一堆,我可以在代碼中看到。 1)頭文件包含在函數內部,所以如果你的頭文件中有一些變量/數組聲明在裏面(我猜你的example_strings在頭文件中),它將成爲函數的局部變量實例,並將佔用堆棧空間。隨着遞歸的繼續,它很快會導致堆棧溢出。

2)爲什麼在每次遞歸調用時都打開文件「output.txt」?所以,每次打電話時你都會一次又一次地打開。將它移到某個地方只能打開一次。

所以,這裏是我的建議(簡短): 1)將標題#include "exstr.h"移出你的函數。

2)不要在每次遞歸調用時打開相同的文件。

  • 微內核
+0

我必須擁有#include在函數中,我的教授認爲調用一個本地頭文件是一個變量,我們不允許使用全局本地頭文件,函數將每個數組的長度和字符串寫入文件中,因爲它必須是一個遞歸函數,這是我能想到的唯一方法。 – Resun 2012-02-10 06:21:02

+1

@Resun調用頭文件是什麼意思?你包含一個頭文件,而不是調用它。 基本上,當編譯器看到你的#include「header_file.h」時,它只是在編譯過程中將header_file.h的內容複製到那個地方。所以,#include「」只是您的標題內容的佔位符。 另外,爲什麼你打開每個調用遞歸output.txt!它嚇壞了我:) – Microkernel 2012-02-10 06:29:07

+1

@Resun只有解決你的問題的方法是,使你的函數迭代或移動你的#include「example_header.h」出你的功能...選擇其中之一,你的問題將被解決:) – Microkernel 2012-02-10 06:30:03