C ++函数不会返回

时间:2010-05-01 16:24:54

标签: c++ class function return

我有一个我正在调用的函数,它一直运行到应该返回但不返回的位置。如果我在函数的最后部分调试某些东西进行调试,它会显示但函数不会返回。

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;
  }

3 个答案:

答案 0 :(得分:4)

你的程序中有一些其他代码行打印“在这里完成”,或者你正在破坏堆栈并影响返回地址。但是我没有在堆栈上看到任何可能超出的缓冲区。

您是否尝试过使用调试器?

答案 1 :(得分:2)

设置断点,在调试器中单步执行,并查看实际执行的开始与您认为应该发生的不同。

通过快速查看代码,您应该得到第二条消息,但是通过调试器查看实际发生的事情将比任何可能出错的理论思考更有帮助。看起来你到处都会泄露内存,我看不到delete的任何new

答案 2 :(得分: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);
   }
相关问题