開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の15章(デバッグと最適化)、15.8(プログラミング実習)、実習 15-1を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
実習15-1.
MacPortsでgdbっていうのをインストール。起動はggdbで、sudoが必要みたい。
コード
sample.c
#include <stdio.h>
#include <memory.h>
#define X_SIZE 60
#define Y_SIZE 32
int matrix1[X_SIZE][Y_SIZE];
int matrix2[X_SIZE][Y_SIZE];
int main()
{
int i, j;
memset(matrix1, -1, sizeof(matrix1));
memset(matrix2, 1, sizeof(matrix2));
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j ++){
printf("%d, %d\n", matrix1[i][j], matrix2[i][j]);
}
}
return (0);
}
makefile
CC=cc CFLAGS=-g sample: sample.c $(CC) $(CFLAGS) -o sample sample.c clean: rm -f sample
入出力結果(Terminal)
$ make
cc -g -o sample sample.c
$ sudo ggdb sample
GNU gdb (GDB) 7.6
Copyright (C) 2013 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 "x86_64-apple-darwin13.0.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /Users/kamimura/Documents/c/practical_c/sample...Reading symbols from /Users/kamimura/Documents/c/practical_c/sample.dSYM/Contents/Resources/DWARF/sample...done.
done.
(gdb) break main
Breakpoint 1 at 0x100000ea5: file sample.c, line 13.
(gdb) run
Starting program: /Users/kamimura/Documents/c/practical_c/sample
Breakpoint 1, main () at sample.c:13
13 memset(matrix1, -1, sizeof(matrix1));
(gdb) print i
$1 = 1606422622
(gdb) print j
$2 = 32767
(gdb) step
14 memset(matrix2, 1, sizeof(matrix2));
(gdb) print matrix1
$3 = {{-1 <repeats 32 times>} <repeats 60 times>}
(gdb) list
9
10 int main()
11 {
12 int i, j;
13 memset(matrix1, -1, sizeof(matrix1));
14 memset(matrix2, 1, sizeof(matrix2));
15 for(i = 0; i < 5; i++){
16 for(j = 0; j < 5; j ++){
17 printf("%d, %d\n", matrix1[i][j], matrix2[i][j]);
18 }
(gdb) print matrix2
$4 = {{0 <repeats 32 times>} <repeats 60 times>}
(gdb) step
15 for(i = 0; i < 5; i++){
(gdb) print matrix2
$5 = {{16843009 <repeats 32 times>} <repeats 60 times>}
(gdb) list
10 int main()
11 {
12 int i, j;
13 memset(matrix1, -1, sizeof(matrix1));
14 memset(matrix2, 1, sizeof(matrix2));
15 for(i = 0; i < 5; i++){
16 for(j = 0; j < 5; j ++){
17 printf("%d, %d\n", matrix1[i][j], matrix2[i][j]);
18 }
19 }
(gdb) break 18
Breakpoint 2 at 0x100000f46: file sample.c, line 18.
(gdb) continue
Continuing.
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
Breakpoint 2, main () at sample.c:19
19 }
(gdb) print matrix1
$6 = {{-1 <repeats 32 times>} <repeats 60 times>}
(gdb) print matrix2
$7 = {{16843009 <repeats 32 times>} <repeats 60 times>}
(gdb) print i
$8 = 0
(gdb) print j
$9 = 5
(gdb) continue
Continuing.
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
Breakpoint 2, main () at sample.c:19
19 }
(gdb) print i
$10 = 1
(gdb) print j
$11 = 5
(gdb) where
#0 main () at sample.c:19
(gdb) step
15 for(i = 0; i < 5; i++){
(gdb) step
16 for(j = 0; j < 5; j ++){
(gdb) continue
Continuing.
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
Breakpoint 2, main () at sample.c:19
19 }
(gdb) continue
Continuing.
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
Breakpoint 2, main () at sample.c:19
19 }
(gdb) continue
Continuing.
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
-1, 16843009
Breakpoint 2, main () at sample.c:19
19 }
(gdb) continue
Continuing.
[Inferior 1 (process 4094) exited normally]
(gdb) continue
The program is not being run.
(gdb) q
$
0 コメント:
コメントを投稿