views:

133

answers:

3

I have a function that I am calling that runs all the way up to where it should return but doesn't return. If I cout something for debugging at the very end of the function, it gets displayed but the function does not return.

fetchData is the function I am referring to. It gets called by outputFile. cout displays "done here" but not "data fetched"

I know this code is messy but can anyone help me figure this out?

Thanks

  //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  A: 

Either there's some other line of code in your program that prints "done here", or you're corrupting the stack and affected the return address. But I don't see any buffers on the stack that you could overrun.

Have you tried using a debugger?

Ben Voigt
Ok, I am using Visual C++ 2008 Express and haven't actually used the debugger before now. I set a breakpoint at cout << "done here" and noticed that the value shown for currentDataPosition is in red. I imagine this means something is wrong there?
Mike
When you run the debugger and reach the breakpoint, do you see any errors? If you step through and hit the return, where does execution go?
peachykeen
Ok at the breakpoint of the return statement I got this error:First-chance exception at 0x77004230 in ext2Interface.exe: 0xC0000005: Access violation reading location 0x00000004.
Mike
Does anyone know how I go about fixing this First-chance exception?
Mike
I was overflowing the data array in the appendData function.
Mike
@Mike: red means the value has changed.
rlbond
+1  A: 

Set breakpoints, step through in a debugger, and see where the actual execution starts to differ from what you think should happen.

From a quick look at your code, you should get the second message, but going through in a debugger and seeing what really happens will help more than any theoretical musings on what could be going wrong. It also looks like you'll be leaking memory everywhere, I don't see any deletes to go with your news.

peachykeen
+1  A: 

If appendData() keeps appending to i_blocks then the data structure keeps growing and so does i_blocks.size(). This will never exit !!!

   //Loop through all the block addresses to get the actual data
   for(i=0; i < i_blocks.size(); i++){
    appendData(i_blocks[i], data);
   }
Romain Hippeau
But the OP says that "done here" is displayed, which means that part finishes.
IVlad
Except Mike said that the "done here" line is displayed, and that printf comes after the call to appendData() and immediately before return.
peachykeen
That part does finish as the others have said. appendData isn't adding more values onto the array. It is setting the values that are already there.
Mike