[gzlug] linux下程序的调试 数据结构

13 views
Skip to first unread message

代晋玮

unread,
May 22, 2012, 12:26:46 PM5/22/12
to gz...@googlegroups.com
大家好,最近学习了数据结构,写一个程序,在linux下运行,gcc编译后,但是没有得到期望的结构,请教各位大侠看一下。
利用栈实现 十进制转换为八进制的程序
源代码如下

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW        -2
#define OK      1
#define ERROR   0
#define TRUE    1
#define FALSE   0

typedef int Status;
typedef int SElemType;
typedef struct{
        SElemType * base;
        SElemType * top;
        int     stacksize;
}SqStack;


Status InitStack(SqStack *s){
        s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
        if(!s->base)
                exit(OVERFLOW);
        s->top=s->base;
        s->stacksize=STACK_INIT_SIZE;
        return OK;
}


Status Push(SqStack *s,SElemType e)
{
        if(s->top - s->base>=s->stacksize){
                s->base=(SElemType *)realloc(s->base,(s->stacksize+ STACKINCREMENT)*sizeof(SElemType));
                if(s->base==NULL)
                        exit(OVERFLOW);
                s->top=s->base+s->stacksize;
                s->stacksize+= STACKINCREMENT;
        }
                *s->top++=e;
        return OK;
}


Status Pop(SqStack *s, SElemType &e)
{
        if(s->top==s->base)
                return ERROR;
        e=*--s->top;
        return OK;
}

Status StackEmpty(SqStack *s)
{
        if(s->top==s->base)                return TRUE;
        else
                return FALSE;
}

void conversion(){
        SqStack *s=NULL;
        SElemType e;
        int N;
        InitStack(&s);
        scanf("%d",&N);
        while(N){
                Push(&s,N%8);
                N=N/8;
        }
        while(!StackEmpty(&s)){
                Pop(&s,&e);
                printf("%d\n",e);
        }
}

int main(void)
{
        conversion();
        return 0;
}

                          

huntxu

unread,
May 22, 2012, 2:18:06 PM5/22/12
to gz...@googlegroups.com
On Wed, 23 May 2012 00:26:46 +0800, 代晋玮 <daiji...@gmail.com> wrote:

> 大家好,最近学习了数据结构,写一个程序,在linux下运行,gcc编译后,但是没有得到期望的结构,请教各位大侠看一下。
你確定可以編譯通過?

> Status Pop(SqStack *s, SElemType &e)
-> Status Pop(SqStack *s, SElemType *e)
> {
> if(s->top==s->base)
> return ERROR;
> e=*--s->top;
-> s->top--
*e = *s->top
> return OK;
> }


> void conversion(){
> SqStack *s=NULL;
s 是 NULL,你後面 initstack 的時候還要賦值 s->base?
-> SqStack s;

可讀性很差,看著累


--
Regards,
Hunt

Liutos

unread,
May 23, 2012, 12:06:06 AM5/23/12
to gz...@googlegroups.com
指针居然用了&?!如果这是C语言的代码的话,那我猜楼主该不会是照书抄的?印象中严蔚敏的数据结构教材就是用的&,不过她的意思其实应该是表示这是一个传入/传出参数。



--
您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。
要向此网上论坛发帖,请发送电子邮件至 gz...@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 gzlug+unsubscribe@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/gzlug?hl=zh-CN 访问此网上论坛。




--
Liutos Love Linux LaTeX Lisp Ling

我的GitHub主页:https://github.com/Liutos

Ma Xiaojun

unread,
May 23, 2012, 2:43:03 AM5/23/12
to gz...@googlegroups.com
YWM的書那些&是借用了C++的引用吧⋯⋯

Liutos

unread,
May 23, 2012, 3:11:07 AM5/23/12
to gz...@googlegroups.com
书上的说法确实是C++的引用的模仿,不过从实际作用来看,就是传入/传出参数吧。

在 2012年5月23日 下午2:43,Ma Xiaojun <damag...@gmail.com>写道:
YWM的書那些&是借用了C++的引用吧⋯⋯

--
您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。
要向此网上论坛发帖,请发送电子邮件至 gz...@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 gzlug+un...@googlegroups.com

若有更多问题,请通过 http://groups.google.com/group/gzlug?hl=zh-CN 访问此网上论坛。

tom mario

unread,
May 22, 2012, 7:54:42 PM5/22/12
to gz...@googlegroups.com
c语言没引用

在 2012年5月23日 上午2:18,huntxu <mhu...@gmail.com>写道:
--
您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。
要向此网上论坛发帖,请发送电子邮件至 gz...@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 gzlug+unsubscribe@googlegroups.com

代晋玮

unread,
May 23, 2012, 8:57:19 AM5/23/12
to gz...@googlegroups.com
不好意思,确实是照着严蔚敏书上抄的。

要取消订阅此网上论坛,请发送电子邮件至 gzlug+un...@googlegroups.com

代晋玮

unread,
May 23, 2012, 9:00:26 AM5/23/12
to gz...@googlegroups.com
gcc 编译通过了,但是运行时不是想要的结果,gdb调试了,但是我找不到是是,问题,请教了。

代晋玮

unread,
May 23, 2012, 9:01:28 AM5/23/12
to gz...@googlegroups.com

是的,gcc通过了,但是运行不是想要的结构。
在 2012年5月23日 上午2:18,huntxu <mhu...@gmail.com>写道:



--
Regards,
Hunt

py_zhu

unread,
May 23, 2012, 9:19:07 AM5/23/12
to gz...@googlegroups.com
不知道下面这个是不是你要的运行结果:
$ ./a.out
13
1
5

修改下面两处(btw 建议多看看书吧):

1.
/* Status Pop(SqStack *s, SElemType &e) */
Status Pop(SqStack *s, SElemType *e)

2.
void conversion(){
        /* SqStack *s=NULL; */
        SqStack *s = {0};
        SElemType e;
        int N;
        InitStack(&s);
        scanf("%d",&N);
        while(N){
                Push(&s,N%8);
                N=N/8;
        }
        while(!StackEmpty(&s)){
                Pop(&s,&e);
                printf("%d\n",e);
        }
}


--
zhu
330.gif

代晋玮

unread,
May 23, 2012, 11:10:57 PM5/23/12
to gz...@googlegroups.com
我运行后 提示 Segmentation fault
是段错误,不知道怎么修改。请教一下。
330.gif

huntxu

unread,
May 23, 2012, 11:29:05 PM5/23/12
to gz...@googlegroups.com
On Thu, 24 May 2012 11:10:57 +0800, 代晋玮 <daiji...@gmail.com> wrote:

> 我运行后 提示 Segmentation fault
> 是段错误,不知道怎么修改。请教一下。
>
>>>>>>
>>>>>> 大家好,最近学习了数据结构,写一个程序,在linux下运行,**gcc编译后,但是没有得到期望的结构,请教各位大侠看一下。
>>>>>>>
>>>>>> 你確定可以編譯通過?
>>>>>>
>>>>>>
>>>>>> Status Pop(SqStack *s, SElemType &e)
>>>>>>>
>>>>>> -> Status Pop(SqStack *s, SElemType *e)
>>>>>>
>>>>>> {
>>>>>>> if(s->top==s->base)
>>>>>>> return ERROR;
>>>>>>> e=*--s->top;
>>>>>>>
>>>>>> -> s->top--
>>>>>> *e = *s->top
>>>>>>
>>>>>>> return OK;
>>>>>>> }
>>>>>>>
>>>>>>
>>>>>>
>>>>>> void conversion(){
>>>>>>> SqStack *s=NULL;
>>>>>>>
>>>>>> s 是 NULL,你後面 initstack 的時候還要賦值 s->base?
>>>>>> -> SqStack s;

請按這樣改試試,謝謝

--
Regards,
Hunt

代晋玮

unread,
May 24, 2012, 8:56:27 AM5/24/12
to gz...@googlegroups.com
解决了,谢谢。



--
Regards,
Hunt

li hex

unread,
May 24, 2012, 11:03:51 AM5/24/12
to gz...@googlegroups.com
$ diff a.c b.c
46c46
< Status Pop(SqStack *s, SElemType *e)
---
> Status Pop(SqStack *s, SElemType &e)
50c50
< *e=*(--s->top);
---
> e=*--s->top;
62c62
< SqStack s={0};
---
> SqStack *s=NULL;

$ ./a.out
106
1
5
2
> --
> 您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。
> 要向此网上论坛发帖,请发送电子邮件至 gz...@googlegroups.com
> 要取消订阅此网上论坛,请发送电子邮件至 gzlug+un...@googlegroups.com

lx

unread,
May 24, 2012, 12:06:54 PM5/24/12
to gz...@googlegroups.com

ulimit,改成能够产生core file,然后使用gdb调试
--
我运行后 提示 Segmentation fault
是段错误,不知道怎么修改。请教一下。
>

代晋玮

unread,
May 24, 2012, 10:24:49 PM5/24/12
to gz...@googlegroups.com
谢谢了,已经好了。

2012/5/24 li hex <lih...@gmail.com>
Reply all
Reply to author
Forward
0 new messages