【求助】编译没有提示错误,程序运行提示崩溃
源代码如下:程序代码:
#include <stdlib.h> #include <stdio.h> #define OK 1 #define ERROR 0 #define MAXSIZE 50 typedef int SElementType; typedef int Status; typedef struct{ SElementType data[MAXSIZE]; int top1; int top2; }SqDoubleStack; void InitStack(SqDoubleStack *S){ S->top1 = -1; S->top2 = MAXSIZE; } Status Push(SqDoubleStack *S, SElementType e, int stackNumber){ if( (S->top2 - S->top1) == 1 ){ return ERROR; } if(stackNumber == 1){ S->data[++S->top1] = e; } else if(stackNumber == 2) { S->data[--S->top2] = e; } return OK; } Status Pop(SqDoubleStack *S,SElementType *e, int stackNumber){ if(stackNumber == 1){ if(S->top1 == -1){ return ERROR; } *e = S->data[S->top1]; S->top1--; } else if (stackNumber == 2){ if(S->top2 == MAXSIZE){ return ERROR; } *e = S->data[S->top2++]; } return OK; } int StackLength(SqDoubleStack *S){ return (S->top1 + 1 + MAXSIZE - S->top2); } int main(){ SqDoubleStack *S = NULL; //SElementType *e = NULL; int i = 0; int stackNumber = 1; Status sts = OK; S = (SqDoubleStack*)malloc(sizeof(SqDoubleStack)); //e = (SElementType*)malloc(sizeof(SElementType)); if((S == NULL) /*|| (e == NULL)*/){ printf("malloc failure."); return 0; } for(i = 0; i < 10; i++){ stackNumber = (i%2==0) ? 1 : 2; sts = Push(S,i+1,stackNumber); printf("push:%d,sts:%d,stackNumber:%d",i+1,sts,stackNumber); } printf("stack double length:%d",StackLength(S)); return 0; }
错误截图如下:
程序调试发现执行到下面这行代码崩溃了
上次也碰到崩溃问题,有大神指点是因为指针变量没有申请内存,这次检查了很多遍,发现有申请,不知道是哪里出问题了