システム
コードブロック12.11+mingwパックの新規インストール。
- win7 64
- gcc 4.7.1
- gdb 7.5
サンプルコード
-gを使用してコンパイルされ、最適化は行われません。
#include <stdio.h>
void foo(int a, int b);
int main() {
foo(400, 42);
return 0;
}
void foo(int a, int b) {
a = a - 10;
b = a + 1;
printf("y2 %d\n", b);
}
問題
「voidfoo(int a、int b)」にブレークポイントを設定し、3行をステップ実行するときにbの値を確認します。コードブロックのデバッグ機能またはgdbコマンドラインのいずれかを使用すると、bの値は391ではなく42になります。コンソール出力は正しい、391。
GDBコマンド
C:\DebugTest>"C:\Program Files (x86)\CodeBlocks\MinGW\bin\gdb.exe"
GNU gdb (GDB) 7.5
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) file bin/Debug/DebugTest.exe
Reading symbols from C:\DebugTest\bin\Debug\DebugTest.exe...done.
(gdb) break foo
Breakpoint 1 at 0x401363: file C:\DebugTest\main.c, line 14.
(gdb) run
Starting program: C:\DebugTest\bin\Debug\DebugTest.exe
[New Thread 3596.0x658]
Breakpoint 1, foo (a=400, b=42) at C:\DebugTest\main.c:14
14 a = a - 10;
(gdb) print b
$1 = 42
(gdb) step
15 b = a + 1;
(gdb) print b
$2 = 42
(gdb) step
17 printf("y2 %d\n", b);
(gdb) print b
$3 = 42
(gdb)
備考
- aとbをmain内のローカル変数として、関数なしで同じ操作を実行すると、デバッグ出力は正しくなります。
- gcc 4.4.1でコンパイルすると、デバッグ出力は正しくなります。
何が間違っている可能性があるか考えていますか?=)