Return not returning!!

Here is the code segment which doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template<class RECORD>
TPointer FileBuf<RECORD>::Read(RECORD& r, const TPointer pos) {
   if (fs.is_open()){
      if (pos != -1) fs.seekg(pos);
      TPointer ret = (*Buffer).Read(fs); // <- Somehow ret is still unassigned
      //TPointer is declared as typedef long TPointer;
      //Buffer is a private attribute declared IOBuffer* Buffer
      r.UnPack(Buffer); //unpack works properly and has nothing to do with ret
      cout << "Ret = "<< ret<< endl; // This prints "Ret = " and not "Ret = 16"
      return ret;
   }
   return -1;
}

//-------------------------------------------------------------
//The method below is located in the template class FixLenBuffer (a son class of IOBuffer)

   TPointer FixLenBuffer::Read(fstream& fs){  //This method is virtual
      TPointer pos = fs.tellg();
      Clear();
      BufferSize = RecordSize;
      fs.read(Buffer, RecordSize);
      cout << "Pos = " << pos << endl; // This prints "Pos = 16"
      return pos; // Why the hell does this return not work?
   }


Tell me if you need more information on any particular variable/class/Etc..
Thanks in advance!!
Last edited on
TPointer ret = (*Buffer).Read(fs); // <- Somehow ret is still unassigned


Look what the Read() function returns. Maybe it returns undefined value if Read was unsuccessful
Actually, when executing the code, i can see on the screen

1
2
Pos = 16
Ret = 


So, "pos" (the value to be returned in Read) is NOT undefined : I was able to print it on the screen ( Pos = 16. Yeah, i wrote the P in caps, but the variable is called pos). However, ret is undefined, and that makes no sense at all!, because
 
TPointer ret = (*Buffer).Read(fs); // <- Somehow ret is still unassigned 

Last edited on
What is TPointer?
Even uninitialized variables have a value
TPointer is declared as typedef long TPointer
EDIT: Bazzy, I'm running this on windows console, and since windows hides the errors in the console, I can see it displaying a window that says "p10.exe has had an error an had to be closed". The error occurs on this line
 
cout << "Ret = "<< ret<< endl;


The execution is aborted when accessing the variable ret.
Last edited on
Try running it through a debugger. If the program really does crap out when trying to access ret (it could be that it just dies before having a chance to completely flush the buffer), you have memory corruption.
Last edited on
Thanks helios. Yeah, it seems the problem is related to that.
When i do r.UnPack(Buffer); the memory goes nuts, and something weird happens to ret (...Curiously i thought UnPack was working properly).

It's very likely it has something to do with the amount of data I'm unpacking on the buffer.
Topic archived. No new replies allowed.