On 27/11/10 00:26, Jupiter wrote:
> Hello,
> I searched the list archives but found nothing relevant to my problem:
> I am using the MinGW packages mentioned in the subject line: g++ 4.5.0-1
> and gdb 7.2-1, downloaded and installed by the automatic installer. As
> IDE I am using Code::Blocks, OS is Windows XP SP3.
> My problem:
> I have a small program in C++. When I try to debug it with gdb, it
> works, in the cmd window and in Code:Blocks. But there is a peculiar
> problem - sometimes the line numbers for the source code, shown in gdb,
> become confused.
> It can happen, that it debugs from source lines 110, 111, 112, 113, 114
> - then suddenly it jumps back to line 110, the 111, and so on. But
> functional the lines are not executed. Nearly every time, several lines
> later it jumps down and is in sync again. There are no loops, btw. Its a
> normal sequence of code. Not always the same code, but the problem is
> repeatable. At certain points in my code the lines come out of sync with
> the source, but the execution seems ok to me - I would have functional
> problems if the lines were executed as shown.
> Here is an example: (In a cmd window with gdb only the same thing
> happens, so its not Code::Blocks)
> Lines of code reported by gdb in Code::Blocks
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:99
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:104
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:105
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:106
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:107
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:105 (???)
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:104 (???)
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:113 (in sync again)
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:98 (???)
> At D:\work\OW Static Tool\src\OWStaticTool.cpp:113 (in sync and end of main)
> Here is the code segment, with line numbers: (I just tried something,
> code makes not much sense, but works well)
> Stepped with 'Step Over':
> 98 std::string statics_file = "\\statics0.mul";
> 99 statics_file = uopath + statics_file;
> 100 void *mem_ptr;
> 101 size_t len;
> 102 try
> 103 {
> 104 file_mapping m_file(statics_file.c_str(), read_only);
> 105 mapped_region region(m_file, read_only);
> 106 mem_ptr = region.get_address();
> 107 len = region.get_size();
> 108 }
> 109 catch (...)
> 110 {
> 111 return 1;
> 112 }
> 113 std::cout<< "Groesse ist "<< len<< " bytes."<< std::endl;
> 114 }<- this is the end of main().
> Anybody seen this?
> Thanks
> CodeDwarf
           Ah! I have seen a very similar problem with the GSL library!

          Are you compiling with optimisations? (e.g. -O2 flag, etc)? In my case,
          I tracked down the problem to the reordering the compiler seems to do
          during optimisation. I am guessing the .line and .text segments get out
          of sync if you use optimisations... Removing the -O2 flag cured the
          problem in my case.


I would agree with Peter Rockett.   I have many years of experience with Fortran programming and have noticed this phenomenon when using debug.
The recommendation was always to compile with optimisation set to its lowest level, normally 0.  This ensures that the lines of code are read in the order in which they appear in source.
I suspect the same is true with C compilers.

Peter Kellas