2010-05-01 119 views
5

我有一個函數,我調用它一直運行到它應該返回但不返回的位置。如果我在函數的最後調出一些用於調試的東西,它會顯示出來,但函數不會返回。C++函數不會返回

fetchData是我所指的功能。它被outputFile調用。 cout顯示「在這裏完成」,但不是「數據提取」

我知道這段代碼很混亂,但任何人都可以幫我弄清楚這一點嗎?

感謝

//Given an inode return all data of i_block data 
    char* fetchData(iNode tempInode){ 
    char* data; 
    data = new char[tempInode.i_size]; 
    this->currentInodeSize = tempInode.i_size; 

    //Loop through blocks to retrieve data 
    vector<unsigned int> i_blocks; 
    i_blocks.reserve(tempInode.i_blocks); 

    this->currentDataPosition = 0; 
    cout << "currentDataPosition set to 0" << std::endl; 
    cout << "i_blocks:" << tempInode.i_blocks << std::endl; 
    int i = 0; 
    for(i = 0; i < 12; i++){ 
    if(tempInode.i_block[i] == 0) 
    break; 
    i_blocks.push_back(tempInode.i_block[i]); 
    } 

    appendIndirectData(tempInode.i_block[12], &i_blocks); 
    appendDoubleIndirectData(tempInode.i_block[13], &i_blocks); 
    appendTripleIndirectData(tempInode.i_block[14], &i_blocks); 

    //Loop through all the block addresses to get the actual data 
    for(i=0; i < i_blocks.size(); i++){ 
    appendData(i_blocks[i], data); 
    } 
    cout << "done here" << std::endl; 

    return data; 
    } 




    void appendData(int block, char* data){ 
    char* tempBuffer; 
    tempBuffer = new char[this->blockSize]; 

    ifstream file (this->filename, std::ios::binary); 
    int entryLocation = block*this->blockSize; 
    file.seekg (entryLocation, ios::beg); 
    file.read(tempBuffer, this->blockSize); 

    //Append this block to data 
    for(int i=0; i < this->blockSize; i++){ 
    data[this->currentDataPosition] = tempBuffer[i]; 
    this->currentDataPosition++; 
    } 
    data[this->currentDataPosition] = '\0'; 
    } 

    void outputFile(iNode file, string filename){ 
    char* data; 
    cout << "File Transfer Started" << std::endl; 
    data = this->fetchData(file); 
    cout << "data fetched" << std::endl; 

    char *outputFile = (char*)filename.c_str(); 
    ofstream myfile; 
    myfile.open (outputFile,ios::out|ios::binary); 
    int i = 0; 
    for(i=0; i < file.i_size; i++){ 
    myfile << data[i]; 
    } 
    myfile.close(); 
    cout << "File Transfer Completed" << std::endl; 
    return; 
    } 
+0

你在做什麼叫「printf'調試」,通常不推薦。你將需要使用一個實際的調試器。 此外,請縮減每個選項卡級別的多個空間。這是不可能讀的...... – rlbond 2010-05-01 16:33:28

+9

@rlbond幾乎所有的調試我都是「printf調試」 - 這種做法絕對沒有錯。 – 2010-05-01 16:34:58

+0

Printf調試非常適用於確定常見的故障領域,但有關詳細信息,調試器可以提供更多幫助。 – ssube 2010-05-01 16:39:35

回答

4

要麼有代碼程序中的一些其他線路上打印「在這裏做」,或者你摧毀棧和受影響的返回地址。但是我沒有看到任何可能超出的緩衝區。

您是否嘗試過使用調試器?

+0

好吧,我正在使用Visual C++ 2008 Express,並且現在還沒有實際使用調試器。我在cout <<「在這裏完成」設置了一個斷點,並注意到currentDataPosition顯示的值爲紅色。我想這意味着有什麼不對? – Mike 2010-05-01 16:42:40

+1

當您運行調試器併到達斷點時,您是否看到任何錯誤?如果你順利完成並返回,那麼執行到哪裏去? – ssube 2010-05-01 16:45:03

+0

好的,在返回語句的斷點處我得到這個錯誤: ext2Interface.exe中0x77004230的第一次機會異常:0xC0000005:訪問衝突讀取位置0x00000004。 – Mike 2010-05-01 17:02:49

2

設置斷點,在調試器中逐步執行,並查看實際執行開始與您認爲應該發生的不同之處。

通過快速查看代碼,您應該看到第二條消息,但是在調試器中查看實際發生的情況將比任何可能出錯的理論思考更有幫助。它也看起來像你會在任何地方泄漏內存,我沒有看到任何delete s去與你的new s。

1

如果appendData()繼續附加到i_blocks,那麼數據結構會不斷增長,i_blocks.size()也會繼續增長。這將永遠不會退出!

//Loop through all the block addresses to get the actual data 
    for(i=0; i < i_blocks.size(); i++){ 
    appendData(i_blocks[i], data); 
    } 
+0

但OP表示顯示「完成此處」,表示該部分完成。 – IVlad 2010-05-01 16:39:17

+0

除Mike表示顯示「done here」行,並且printf在appendData()調用之後並且在返回之前立即出現。 – ssube 2010-05-01 16:41:17

+0

該部分完成了其他人的說法。 appendData不會將更多值添加到數組中。它正在設置已經存在的值。 – Mike 2010-05-01 16:45:58