GDB Linux GNU Linux Distribution. gdb gcc g++ -g gdb EB_01.cpp

Similar documents
Program Design: Using the Debugger

GDB QUICK REFERENCE GDB Version 4

ECE/ME/EMA/CS 759 High Performance Computing for Engineering Applications

Using gdb to find the point of failure

The Dynamic Debugger gdb

Debug for GDB Users. Action Description Debug GDB $debug <program> <args> >create <program> <args>

CSE 410: Systems Programming

GDB 1 GDB 2 GDB. Fortran Pascal GDB 4. hoge.c. Fig. 1. calc.c. Fig GDB. GDB Debian. # apt-get install gdb

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Exercise Session 6 Computer Architecture and Systems Programming

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

CMPSC 311- Introduction to Systems Programming Module: Debugging

CMPSC 311- Introduction to Systems Programming Module: Debugging

CSC Data Structures, Fall, 2009

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

Data and File Structures Laboratory

UNIX Makefile. C Project Library Distribution and Installation.

ARM DS-5. Using the Debugger. Copyright 2010 ARM. All rights reserved. ARM DUI 0446A (ID070310)

Code Review and Debugging (Lab 05)

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

CS/COE 0449 term 2174 Lab 5: gdb

Tips on Using GDB to Track Down and Stamp Out Software Bugs

1 A Brief Introduction To GDB

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

ARM DS-5. Debugger Command Reference. Copyright 2010 ARM. All rights reserved. ARM DUI 0452A (ID070310)

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Computer Labs: Debugging

Programming Tips for CS758/858

Chapter - 17 Debugging and Optimization. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 1

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

DS-5 ARM. Debugger Command Reference. Version 5.3. Copyright 2010 ARM. All rights reserved. ARM DUI 0452C (ID112910)

CSE 351. GDB Introduction

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

Problem Set 1: Unix Commands 1

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Introduction to GDB. Lezione 9 (taken from Owen HSU material)

Red Hat Developer Tools

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Using the Command Line

CS354 gdb Tutorial Written by Chris Feilbach

Red Hat Developer Tools

Scientific Programming in C IX. Debugging

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference for x86-64 Assembly Language

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

dbx90: Fortran debugger March 9, 2009

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on

Section 1: Tools. Kaifei Chen, Luca Zuccarini. January 23, Make Motivation How... 2

PROGRAMMAZIONE I A.A. 2017/2018

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

Lab: Supplying Inputs to Programs

EE 355 Lab 3 - Algorithms & Control Structures

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

GDB cheatsheet - page 1

Introduction to Programming

omp critical #pragma omp critical x = x + 1; !$OMP CRITICAL X = X + 1!$OMP END CRITICAL

DS-5 ARM. Debugger Command Reference. Version Copyright ARM. All rights reserved. ARM DUI 0452P (ID091713)

Collect Linux Hardware Trace for ARMv8 User Space and Kernel Space Applications

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Using the GNU Debugger

Using the GNU Debugger

Review: Expressions, Variables, Loops, and more.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Programming Studio #9 ECE 190

do { statements } while (condition);

Understanding the Program Run

Using the Xcode Debugger

Intro to Segmentation Fault Handling in Linux. By Khanh Ngo-Duy

CS3210: Tutorial Session 2. Kyuhong Park-- edited by Kyle Harrigan

CS356: Discussion #5 Debugging with GDB. Marco Paolieri

IDE: Integrated Development Environment

The Linux Programming Environment. Computer Science Department Texas State University

Disassemble the machine code present in any memory region. Single step through each assembly language instruction in the Nios II application.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 17 - Notes Recursion

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

DEBUGGING: OBSERVING AND TRACKING

Source level debugging. October 18, 2016

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

Debugging uclinux on Coldfire

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

GDB Tutorial. University of Waterloo

Today s presentation. Git gdb Project 1

Name SECTION: 12:45 2:20. True or False (12 Points)

gdbtui - Linux Command

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 5

Debugging. John Lockman Texas Advanced Computing Center

CS101 Linux Shell Handout

ALD Assembly Language Debugger Copyright (C) Patrick Alken

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Why Is Repetition Needed?

Debugging. P.Dagna, M.Cremonesi. May 2015

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSCE 206: Structured Programming in C++

Using a debugger. Segmentation fault? GDB to the rescue!

Transcription:

B Linux GDB

GDB Linux GNU GPL Linux Distribution Linux E-B.1 gcc g++ -g EB_01.cpp EB_01.cpp E/EB/EB_01.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /**** :EB_01.cpp : *****/ #include <iostream> using namespace std; void cal_ele(int i,int *add_ary,int *mul_ary); void main(void) { int add_ary[5]={0}; int mul_ary[5]={1}; int i; for(i=1;i<10;i++) cal_ele(i,add_ary,mul_ary); for(i=0;i<5;i++) cout << "add_ary[" << i << "]=" << add_ary[i] << endl; cout << "---------------------\n"; for(i=0;i<5;i++) cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; } void cal_ele(int i,int *add_ary,int *mul_ary) { E-B-2

Linux GDB B 20 21 22 } add_ary[i]=add_ary[i-1]+i; mul_ary[i]=mul_ary[i-1]*i; add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 segment fault (1) 0~4 add_ary 0!~4! mul_ary (2) add_ary 0,1,3,6,10 segment fault -g EB_01 g++ -g EB_01.cpp -o EB_01 (3) -g EB_01 E-B-3

E-B.2 -g EB_01 jhchen@aho:~/c_cpp/e/eb$ EB_01 GNU Red Hat Linux 7.x (5.0rh-15) (MI_OUT) Copyright 2001 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... () GDB (1) GDB EB_01 (2) () GDB (3) -q quit E-B-4

Linux GDB B () quit jhchen@aho:~/c_cpp/e/eb$ Tab qu Tab quit E-B.3 list run shell Linux shell GDB exit shell linux Linux shell shell ls help help all list list () list 1 /**** :EB_01.cpp : *****/ 2 #include <iostream> 3 using namespace std; 4 void cal_ele(int i,int *add_ary,int *mul_ary); 5 void main(void) 6 { E-B-5

7 int add_ary[5]={0}; 8 int mul_ary[5]={1}; 9 int i; 10 for(i=1;i<10;i++) () list 11 cal_ele(i,add_ary,mul_ary); 12 for(i=0;i<5;i++) 13 cout << "add_ary[" << i << "]=" << add_ary[i] << endl; 14 cout << "---------------------\n"; 15 for(i=0;i<5;i++) 16 cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; 17 } 18 void cal_ele(int i,int *add_ary,int *mul_ary) 19 { 20 add_ary[i]=add_ary[i-1]+i; () list 21 mul_ary[i]=mul_ary[i-1]*i; 22 } () quit jhchen@aho:~/c_cpp/e/eb$ (1) list 10 EB_01 22 (2) 3 list () list 7,19 7 int add_ary[5]={0}; 8 int mul_ary[5]={1}; 9 int i; 10 for(i=1;i<10;i++) 11 cal_ele(i,add_ary,mul_ary); 12 for(i=0;i<5;i++) 13 cout << "add_ary[" << i << "]=" << add_ary[i] << endl; 14 cout << "---------------------\n"; E-B-6

Linux GDB B 15 for(i=0;i<5;i++) 16 cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; 17 } 18 void cal_ele(int i,int *add_ary,int *mul_ary) 19 { () quit jhchen@aho:~/c_cpp/e/eb$ 7~19 list 7,19 run run () run Starting program: /root/c_cpp/e/eb/eb_01 add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 Program received signal SIGSEGV, Segmentation fault. 0x00000015 in?? () () quit A debugging session is active. Do you still want to close the debugger?(y or n) y E-B-7

run run EB_01 Ctrl + C shell Linux Shell GDB GDB Linux Shell shell Linux Shell () shell jhchen@aho:~/c_cpp/e/eb$ ls EB_01 EB_01.cpp EB_01_1.cpp EB_02.cpp jhchen@aho:~/c_cpp/e/eb$ exit exit () quit jhchen@aho:~/c_cpp/e/eb$ shell Linux Shell GDB GDB shell exit GDB quit shell shell linux Linux shell Linux Shell GDB GDB Linux Shell shell () shell ls EB_01 EB_01.cpp EB_01_1.cpp EB_02.cpp () quit jhchen@aho:~/c_cpp/e/eb$ E-B-8

Linux GDB B GDB shell ls Linux shell ls help help () help run Start debugged program. You may specify arguments to give it. Args may include "*", or "[...]"; they are expanded using "sh". Input and output redirection with ">", "<", or ">>" are also allowed. With no arguments, uses arguments last specified (with "run" or "set args"). To cancel previous arguments and run with no arguments, use "set args" without arguments. () quit jhchen@aho:~/c_cpp/e/eb$ GDB help run run help all all () help all ni -- Step one instruction si -- Step one instruction exactly E-B-9

stepping -- Specify single-stepping behavior at a tracepoint tp -- Set a tracepoint at a specified line or function or address where -- Print backtrace of all stack frames ws -- Specify single-stepping behavior at a tracepoint append binary -- Append target code/data to a raw binary file append memory -- Append contents of memory to a raw binary file append value -- Append the value of an expression to a raw binary file awatch -- Set a watchpoint for an expression break -- Set breakpoint at specified line or function catch -- Set catchpoints to catch events clear -- Clear breakpoint at specified line or function commands -- Set commands to be executed when a breakpoint is hit condition -- Specify breakpoint number N to break only if COND is true delete -- Delete some breakpoints or auto-display expressions disable -- Disable some breakpoints enable -- Enable some breakpoints hbreak -- Set a hardware assisted breakpoint ignore -- Set ignore-count of breakpoint number N to COUNT rbreak -- Set a breakpoint for all functions matching REGEXP rwatch -- Set a read watchpoint for an expression tbreak -- Set a temporary breakpoint ---Type <return> to continue, or q <return> to quit--- q Enter Enter E-B.4 E-B-10

Linux GDB B GDB break break break watch break () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () quit A debugging session is active. Do you still want to close the debugger?(y or n) y jhchen@aho:~/c_cpp/e/eb$ (1) break 11 11 (2) run 11 11 E-B-11

break () break cal_ele Breakpoint 1 at 0x80487e1: file EB_01.cpp, line 20. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, cal_ele (i=1, add_ary=0xbffffaa8, mul_ary=0xbffffa94) at EB_01.cpp:20 20 add_ary[i]=add_ary[i-1]+i; () (1) break cal_ele cal_ele (2) run cal_ele (3) cal_ele mul_ary=0xbffffa94) (i=1, add_ary=0xbffffaa8, break 100 100 EB_01 11 9 i>=5 E-B-12

Linux GDB B () break 11 if i>=5 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () break 11 if i>=5 i>=5 11 () break cal_ele if i>=5 Breakpoint 1 at 0x80487e1: file EB_01.cpp, line 20. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, cal_ele (i=5, add_ary=0xbffffaa8, mul_ary=0xbffffa94) at EB_01.cpp:20 20 add_ary[i]=add_ary[i-1]+i; () break cal_ele if i>=5 cal_ele i>=5 watch GDB watch point watch point break point GDB E-B-13

watch point watch point watch point watch i > 4 EB_02.cpp EB_02.cpp E/EB/EB_02.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /**** :EB_02.cpp : *****/ #include <iostream> using namespace std; void main(void) { int a,b,i; a=0; b=0; for(i=1;i<10;i++) { a=b+10; b=a+10; a=b+25; b=a+100; } cout << "a=" << a << "\t" << "b=" << b << endl; } a>500 break point 11 13 watch point jhchen@aho:~/c_cpp/e/eb$ g++ -g EB_02.cpp -o EB_02 jhchen@aho:~/c_cpp/e/eb$ -q EB_02 () watch a > 500 No symbol "a" in current context. () break 8 Breakpoint 1 at 0x804865d: file EB_02.cpp, line 8. () run E-B-14

Linux GDB B Starting program: /root/c_cpp/e/eb/eb_02 Breakpoint 1, main () at EB_02.cpp:8 8 b=0; () watch a > 500 Hardware watchpoint 2: a > 500 () continue Continuing. Hardware watchpoint 2: a > 500 Old value = false New value = true main () at EB_02.cpp:12 12 b=a+10; () (1) watch a>500 GDB a a 6 (2) break point 8 break 8 GDB 8 a (3) watch a>500 GDB GDB a (4) continue GDB continue watch point 12 a>500 11 E-B.5 GDB break point watch point E-B-15

print whatis () break 11 if add_ary[0]>100 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $1 = 6 () whatis i type = int () (1) add_ary add_ary 0,1,3,6,10 100 (120,720,5040,40320,362880) add_ary[0]>100 (2) run add_ary[0]>100 (3) i print i (4) print i GDB $1=6 i 6 (5) i whatis i GDB i i int E-B-16

Linux GDB B E-B.6 continue step next () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $1 = 1 () continue Continuing. Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $2 = 2 () next Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $3 = 3 () step cal_ele (i=3, add_ary=0xbffffaa8, mul_ary=0xbffffa94) at EB_01.cpp:20 E-B-17

20 add_ary[i]=add_ary[i-1]+i; () step 21 mul_ary[i]=mul_ary[i-1]*i; () (1) 11 (2) run 11 (3) continue 10 11 (4) next (5) step cal_ele 20 (6) step 21 E-B.7 break clear delete info breakpoints disable enable E-B-18

Linux GDB B info breakpoints info breakpoints () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break 22 Breakpoint 2 at 0x8048833: file EB_01.cpp, line 22. () break cal_ele Breakpoint 3 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x08048833 in cal_ele(int, int *, int *) at EB_01.cpp:22 3 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () break 3 info breakpoints GDB Num Type breakpoint watchpoint Disp Enb Address What dis: keep: y: n: E-B-19

() run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () info breakpoint Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 breakpoint already hit 1 time 2 breakpoint keep y 0x08048833 in cal_ele(int, int *, int *) at EB_01.cpp:22 3 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () 1 breakpoint already hit 1 time clear clear () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 E-B-20

Linux GDB B () clear 11 Deleted breakpoint 1 () info breakpoints Num Type Disp Enb Address What 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () clear cal_ele Deleted breakpoint 2 () info breakpoints No breakpoints or watchpoints. () (1) clear 11 11 (2) clear cal_ele cal_ele clear delete delete () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 E-B-21

() delete 2 () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 () delete 2 2 break cal_ele disable disable () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () disable 2 () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep n 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () disable 2 2 Enb n E-B-22

Linux GDB B disable enable enable () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () disable () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep n 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep n 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () enable 2 () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep n 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () enable 2 2 Enb y E-B-23

enable enable once GDB enable once () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 () enable once 1 () info breakpoints Num Type Disp Enb Address What 1 breakpoint dis y 0x08048688 in main at EB_01.cpp:11 () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () info breakpoints Num Type Disp Enb Address What 1 breakpoint dis n 0x08048688 in main at EB_01.cpp:11 breakpoint already hit 1 time () continue Continuing. add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 E-B-24

Linux GDB B add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 Program received signal SIGSEGV, Segmentation fault. 0x00000015 in?? () () (1) break 11 Disp keep Enb y (2) enable once 1 1 (3) Disp dis Enb y (4) run (5) Disp dis Enb n (6) continue E-B.8 1 EB_01.cpp E-B-25

jhchen@aho:~/c_cpp/e/eb$ g++ EB_01.cpp -o EB_01 jhchen@aho:~/c_cpp/e/eb$./eb_01 add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 Segment fault jhchen@aho:~/c_cpp/e/eb$ 2 Segment fault add_ary jhchen@aho:~/c_cpp/e/eb$ g++ -g EB_01.cpp -o EB_01 -g 3 GDB () 4 add_ary add_ary cal_ele 11 add_ary[0]>100 add_ary 100 () break 11 if add_ary[0]>100 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. E-B-26

Linux GDB B 5 run () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); 6 11 add_arr[0] 100 100 i () print i $1 = 6 7 i=6 add_arr[0] add_arr[6] mul_arr[6] GDB 5 () quit A debugging session is active. Do you still want to close the debugger?(y or n) y 8 EB_01_1.cpp 1 2 3 4 5 /**** :EB_01_1.cpp : *****/ #include <iostream> using namespace std; void cal_ele(int i,int *add_ary,int *mul_ary); void main(void) E-B-27

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 { int add_ary[5]={0}; int mul_ary[5]={1}; int i; for(i=1;i<5;i++) cal_ele(i,add_ary,mul_ary); for(i=0;i<5;i++) cout << "add_ary[" << i << "]=" << add_ary[i] << endl; cout << "---------------------\n"; for(i=0;i<5;i++) cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; } void cal_ele(int i,int *add_ary,int *mul_ary) { add_ary[i]=add_ary[i-1]+i; mul_ary[i]=mul_ary[i-1]*i; } 9 EB_01_1.cpp Segment fault jhchen@aho:~/c_cpp/e/eb$ g++ EB_01_1.cpp -o EB_01_1 jhchen@aho:~/c_cpp/e/eb$./eb_01_1 add_ary[0]=0 add_ary[1]=1 add_ary[2]=3 add_ary[3]=6 add_ary[4]=10 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 E-B-28