2010-05-01 90 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