为啥devc++程序运行正确返回不为0?

#include <stdio.h>#include <stdlib.h>typedef char ElemType;typedef struct BiTNode{ char data; struct BiTNode *lchild; struct BiTNode *rchild; int DescNum;}BiTNode ,*BiTree;void CreateBiTree(BiTree *T) { char ch; scanf("%c",&ch); if(ch==' '){ *T=NULL; } else { *T=(BiTNode*)malloc(sizeof(BiTNode*)); (*T)->data=ch; CreateBiTree(&(*T)->lchild) ; CreateBiTree(&(*T)->rchild) ; }}void visit(char c){ printf("%c",c);}int PreOrderTraverse(BiTree T,int count){ int p; if(T) { p=count; visit(T->data); count=PreOrderTraverse(T->lchild,count); count=count+PreOrderTraverse(T->rchild,p); T->DescNum=count; count=count+1; return count; }} void ReadD(BiTree T){ if(T) { printf("%d",T->DescNum); ReadD(T->lchild); ReadD(T->rchild); }} int main(){ BiTree T; int count=0; printf("请按先序遍历输入二叉树:"); CreateBiTree(&T); printf("先序遍历:\n"); PreOrderTraverse(T,count); printf("\n按先序遍历读取DescNum:\n"); ReadD(T); return 0;}

返回值不为0说明你的程序在中途就触发了SIGSEGV信号挂了,根本没能执行到主函数结尾的return 0。出现这个问题的原因是你访问了非法内存,例如数组越界、解引用了未初始化的指针、多次释放同一地址的内存空间等,请仔细检查你的程序,可以通过gdb调试确定异常位置。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-04-09
因为返回的是默认的主函数返回值的地址,代码段起始和末尾(即主函数返回值的地址,此处称“0地址”)之间是有一定间隔的,在程序地址段未用尽(所使用的地址在间隔以内)时,可以正常返回“0地址”,但是程序地址段用尽时(超出“0地址”时),返回此时被占用的“0地址”的内存地址。
相似回答