C++函数不会返回

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

最佳答案

要么您的程序中有其他代码行打印“此处完成”,要么您破坏了堆栈并影响了返回地址。但我没有在堆栈上看到任何您可能会溢出的缓冲区。

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

关于C++函数不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2750680/

相关文章:

返回所有排列的 JavaScript 函数

c++ - 使用图权重 boost 深度优先访问者最小生成树

c++ - 编译后自动设置文件版本

java - 新手 - 初始化 Java 类

android - 如何将主要 Activity 和日期选择器分离到自己的类(class)

function - 如何在另一个返回 Refcursor 集的函数中使用 postgresql 函数

c# - 使用来自其他库的值的 Swig 枚举

c++ - 当我尝试发出目标代码时,为什么 LLVM 会出现段错误?

c++ - 获取用户定义类的实例

r - 编写自己的函数时如何使用R的省略号功能?