c++在cygwin下gdb调试时出现的问题 编译没问题。学生党已经无力搞这个了,只能上这里来求救

hiro方圆 2015-10-23 08:36:36
这是代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<cstring>

using namespace std;
typedef char status;

typedef struct lnode
{
struct lnode *next;
struct lnode *down;
char data[20];
char name[20];
char sex[20];
}lnode, *linklist;

void creatlist_zy(linklist &l, int n);
void creatlist_nj(linklist &l, int n);
void creatlist_bj(linklist &l, int n);
void creatlist_stu(linklist &l, int n);
void insert_zy(linklist &l);
void insert_nj(linklist &l);
void insert_bj(linklist &l);
void insert_stu(linklist &l);

void creatlist_zy(linklist &l, int n) {
linklist p, q;
int m;
l = (linklist) malloc (sizeof (lnode));
l -> next = NULL;
l -> down = NULL;
for (int i = n; i > 0; -- i)
{
p = (linklist) malloc (sizeof (lnode));
cout << "输入专业" << i << ":";
cin >> p -> data;
p -> next = l -> next;
l -> next = p;
cout << "请输入该专业年级数目:" ;
cin >> m;
creatlist_nj(q, m);
p -> down = q;
}
}

void creatlist_nj(linklist &l, int n) {
linklist p, q;
int m;
l = (linklist) malloc (sizeof (lnode));
l -> next = NULL;
l -> down = NULL;
for (int i = n; i > 0; -- i)
{
p = (linklist) malloc (sizeof (lnode));
cout << "输入年级" << i << ":";
cin >> p -> data;
p -> next = l -> next;
l -> next = p;

cout << "请输入该年级班级数目:";
cin >> m;
creatlist_bj(q, m);
p -> down = q;
}
}

void creatlist_bj(linklist &l, int n) {
linklist p;
linklist q;
int m;
l = (linklist) malloc (sizeof (lnode));
l -> next = NULL;
l -> down = NULL;
for (int i = n; i > 0; -- i)
{
p = (linklist) malloc (sizeof (lnode));
cout << "输入班级" << i << ":";
cin >> p -> data;
p -> next = l -> next;
l -> next = p;

cout << "请输入班级人数目:";
cin >> m;
creatlist_stu(q, m);
q = p -> down;
}
}

void creatlist_stu(linklist &l, int n) {
linklist p;

l = (linklist) malloc (sizeof (lnode));
l -> next = NULL;
l -> down = NULL;
for (int i = n; i > 0; -- i)
{
p = (linklist) malloc (sizeof (lnode));
cout << "姓名" << i << ":";
cin >> p -> name;
cout << "性别" << i << ":";
cin >> p -> sex;
p -> next = l ->next;
l -> next = p;
}
}

void insert_zy(linklist &l){
linklist p,q;
p = l-> next;
char zym[20];
cout << "专业名:";
cin >> zym;
while (p)
{
if (strcmp(p -> data, zym) == 0)
{
q = p -> down;

insert_nj(q);
}
p = p -> next;
if (p = NULL)
{
cout << "输入新的专业:" << endl;
creatlist_nj(q,1);
}
}

}

void insert_nj(linklist &l){
linklist p,q;
p = l -> next;

char njm[20];
cout << "年级名:";
cin >> njm;
while (p)
{
if (strcmp(p -> data, njm) == 0)
{
q = p -> down;

insert_bj(q);
}
p = p -> next;
if (p = NULL)
{
cout << "输入新的年级:" << endl;
creatlist_nj(q,1);
}
}
}

void insert_bj(linklist &l){
linklist p;
linklist q;
p = l -> next;
char bjm[20];
cout << "班级名:";
cin >> bjm;
while (p)
{
if (strcmp(p -> data, bjm) == 0)
{
q = p -> down;

insert_stu(q);
}
p = p -> next;
if (p = NULL)
{
cout << "输入新的班级:" << endl;
creatlist_nj(q,1);
}
}
}

void insert_stu(linklist &l){
linklist p;
p = (linklist) malloc (sizeof(lnode));
l = l -> next;
cout << "姓名:";
cin >> p -> name;
cout << "性别:";
cin >> p -> sex;
p -> next = l -> next;
p = l -> next;
cout << "插入成功!" << endl;
}

int main() {
linklist l;
int n,choice;


cout << "你想要进行的操作:1:新建学生表;2:插入学生信息;" <<endl;
cin >> choice;
while (1)
{
switch (choice)
{
case 1:
cout << "输入专业数目:";
cin >> n;
creatlist_zy(l,n);
break;
case 2:

insert_zy(l);
break;
default: cout << "请输入1,2!" <<endl; break;
}

cout << "你想要进行的操作:1:新建学生表;2:插入学生信息;" <<endl;
cin >> choice;

}


return 0;
}




一下是gdb:
GNU gdb (GDB) 7.8
Copyright (C) 2014 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-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from stu.exe...done.
(gdb) break insert_stu
Breakpoint 1 at 0x1004017c1
(gdb) r
Starting program: /cygdrive/d/cygwin/home/cppwork/stu.exe
[New Thread 1928.0x7d0]
[New Thread 1928.0x1bd4]
[New Thread 1928.0xec8]
[New Thread 1928.0x2380]
[New Thread 1928.0x21bc]
你想要进行的操作:1:新建学生表;2:插入学生信息;
1
输入专业数目:1
输入专业1:jisuanji
请输入该专业年级数目:1
输入年级1:daer
请输入该年级班级数目:sa[New Thread 1928.0x1154]
[New Thread 1928.0x173c] Quit
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /cygdrive/d/cygwin/home/cppwork/stu.exe
[New Thread 3144.0x1944]
[New Thread 3144.0x200c]
[New Thread 3144.0x204]
[New Thread 3144.0x17a8]
[New Thread 3144.0x22cc]
你想要进行的操作:1:新建学生表;2:插入学生信息;
1
输入专业数目:1
输入专业1:jisuanji
请输入该专业年级数目:1
输入年级1:daer
请输入该年级班级数目:1
输入班级1:sanban
请输入班级人数目:1
姓名1:hiro
性别1:nan
你想要进行的操作:1:新建学生表;2:插入学生信息;
2
专业名:jisuanji
年级名:[New Thread 3144.0xc90]
[New Thread 3144.0xad0]
daer
班级名:
[Thread 3144.0x17a8 exited with code 0]
[Thread 3144.0x204 exited with code 0]
[Thread 3144.0x200c exited with code 0]
sanban

Breakpoint 1, 0x00000001004017c1 in insert_stu(lnode*&) ()
(gdb) n
Single stepping until exit from function _Z10insert_stuRP5lnode,
which has no line number information.
姓名:fangyuan
性别:nan

Program received signal SIGSEGV, Segmentation fault.
0x000000010040182e in insert_stu(lnode*&) ()
...全文
268 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fefe82 2015-10-24
  • 打赏
  • 举报
回复
如果你的程序有调试信息的话,可以用 list 看到当前的代码,用 up down 在调用栈上移动, p 可以看到内存中变量的内容等。这些可以帮助你分析你的程序哪里错了。 更多的需要去看 gdb 的文档。
苦逼码农 2015-10-24
  • 打赏
  • 举报
回复
卧槽, 加-g参数 gcc -o test -g -Wall test.c 搞成debug版本,然后编译,再运行, 如果程序崩溃, 会生成core文件。 假设core的名字叫 core1000 那么就你就 gdb test core1000 ,这样就可以看到崩溃的堆栈了。 前提是:你要开启可以生成core
hiro方圆 2015-10-24
  • 打赏
  • 举报
回复
引用 1 楼 fefe82 的回复:
bt 可以打印出 stack trace

然后呢?还是不懂
hiro方圆 2015-10-24
  • 打赏
  • 举报
回复
苦逼码农 2015-10-24
  • 打赏
  • 举报
回复
cygwin 不知道可以生成core文件不,如果不可以的话,就比较麻烦。 我上面说的都是解决问题的思路
fefe82 2015-10-23
  • 打赏
  • 举报
回复
bt 可以打印出 stack trace

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧