函数空间分配问题
[bo]问题:Program has received a signal-name="SIGSEGV",signal-meaning="Segmentation fault" at Push({name="S",value="0x0"},{name="e",value="35"}), file C:\Program\MinGWStudio\Templates\EvaluateExpression\EvaluateExpression.c:66[/bo]程序:
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct{
int *base;//存储空间初始分配量
int *top;//栈顶指针
int stacksize;//当前已分配的存储空间,以元素为单位
}SqStack;
SqStack *OPTR,*OPND;//全局变量OPTR和OPND分别为运算符栈和运算数栈
//以下InitStack(),Push (),Pop(),Emptys()及GetTop()函数均借鉴数据结构(C语言版)教材
Status InitStack(SqStack *S){
//构造一个空栈S
S=(SqStack *)malloc(sizeof(SqStack));
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base)exit(OVERFLOW);//存储分配失败
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}//InitStackz
Status Push (SqStack *S,int e){
//插入元素e为新的栈顶元素
if(S->top-S->base>=S->stacksize){//栈满,追加存储空间
S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base)exit(OVERFLOW);//存储分配失败
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}//if
*S->top++=e;
return OK;
}//Push
Status Pop(SqStack *S,int e){
//若栈不空,则删除S的栈顶元素,用e返回其值
if(S->top==S->base)return ERROR;
e=*--S->top;
return (e);
}//Pop
Status Emptys(SqStack *S){
//若栈空,返回TRUE,否则返回FALSE
if(S->top==S->base)
return TRUE;
else
return FALSE;
}
Status GetTop(SqStack *S){
//若栈不空,则用e返回S的栈顶元素
int e;
if(S->top==S->base)return ERROR;
e=*(S->top-1);
return(e);
}//GetTop
char Precede (char ch1,char ch2){
//构造算符间的优先关系,优先关系借鉴数据结构(C语言版)教材
if(ch1=='+'||ch1=='-')
{
if(ch2=='+'||ch2=='-'||ch2==')'||ch2=='#')return('>');
else if(ch2=='*'||ch2=='/'||ch2=='(')return('<');
}
else if(ch1=='*'||ch1=='/')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2==')'||ch2=='#')return('>');
else if(ch2=='(')return('<');
}
else if(ch1=='(')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='(')return('<');
else if(ch2==')')return('=');
}
else if(ch1==')')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2==')'||ch2=='#')return('>');
//else if()return('=')
}
else if(ch1=='#')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='(')return('<');
else if(ch2=='#')return('=');
}
else if(!((ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='('||ch1==')'||ch1=='#')&&(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='('||ch2==')'||ch2=='#')))
//非算符退出
return ERROR;
}
Status Operate(int a,char theta,int b){
//四则运算
if(theta=='+')return (a+b);
else if(theta=='-')return (a-b);
else if(theta=='*')return (a*b);
else if(theta=='/')return (a/b);
}//Operate
Status In(char c){
//判断c是否是运算符,如果是返回OK,否则返回ERROR。
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#')
return OK;
else
return ERROR;
}//In
Status EvaluateExpression(){
//算术表达式求值的算符优先算法。
int a,b,c,x;
char theta;
InitStack(OPTR);Push(OPTR,'#');
InitStack(OPND);c=getchar();
while(c!='#'||GetTop(OPTR)!='#'){
if(!In(c)){
Push((OPND),c);c=getchar();
}
else{
switch(Precede(GetTop(OPTR),c)){
case'<'://栈顶元素优先级低
Push(OPTR,c);c=getchar();break;
case'='://脱括号并接收下一字符
Pop(OPTR,x);c=getchar();break;
case'>'://退栈并将运算结果入栈
Pop(OPTR,theta);
Pop(OPND,b);Pop(OPND,a);
Push(OPND,Operate(a,theta,b));break;
}//switch
}//else
}//while
return GetTop(OPND);
}//EvaluateExpression
int main(){
printf("Input the EvaluateExpression");
printf("The result is: %d",EvaluateExpression());
free(OPTR);free(OPND);
return 0;
}
----------------解决方案--------------------------------------------------------
[bo]急!!![/bo]
大哥大姐们帮帮忙,为什么会出现 [bo]Segmentation fault at Push[/bo]
谢谢!
----------------解决方案--------------------------------------------------------
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct{
int *base;//存储空间初始分配量
int *top;//栈顶指针
int stacksize;//当前已分配的存储空间,以元素为单位
}SqStack;
SqStack *OPTR,*OPND;//全局变量OPTR和OPND分别为运算符栈和运算数栈
//以下InitStack(),Push (),Pop(),Emptys()及GetTop()函数均借鉴数据结构(C语言版)教材
SqStack * InitStack(void){ //*****修改此函数;
//构造一个空栈S
SqStack *S;
S=(SqStack *)malloc(sizeof(SqStack));
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base)exit(OVERFLOW);//存储分配失败
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return S;
}//InitStackz
Status Push (SqStack *S,int e){
//插入元素e为新的栈顶元素
if(S->top-S->base>=S->stacksize){//栈满,追加存储空间
S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base)exit(OVERFLOW);//存储分配失败
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}//if
*S->top++=e;
return OK;
}//Push
Status Pop(SqStack *S,int e){
//若栈不空,则删除S的栈顶元素,用e返回其值
if(S->top==S->base)return ERROR;
e=*--S->top;
return (e);
}//Pop
Status Emptys(SqStack *S){
//若栈空,返回TRUE,否则返回FALSE
if(S->top==S->base)
return TRUE;
else
return FALSE;
}
Status GetTop(SqStack *S){
//若栈不空,则用e返回S的栈顶元素
int e;
if(S->top==S->base)return ERROR;
e=*(S->top-1);
return(e);
}//GetTop
char Precede (char ch1,char ch2){
//构造算符间的优先关系,优先关系借鉴数据结构(C语言版)教材
if(ch1=='+'||ch1=='-')
{
if(ch2=='+'||ch2=='-'||ch2==')'||ch2=='#')return('>');
else if(ch2=='*'||ch2=='/'||ch2=='(')return('<');
}
else if(ch1=='*'||ch1=='/')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2==')'||ch2=='#')return('>');
else if(ch2=='(')return('<');
}
else if(ch1=='(')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='(')return('<');
else if(ch2==')')return('=');
}
else if(ch1==')')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2==')'||ch2=='#')return('>');
//else if()return('=')
}
else if(ch1=='#')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='(')return('<');
else if(ch2=='#')return('=');
}
else if(!((ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='('||ch1==')'||ch1=='#')&&(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='('||ch2==')'||ch2=='#')))
//非算符退出
return ERROR;
}
Status Operate(int a,char theta,int b){
//四则运算
if(theta=='+')return (a+b);
else if(theta=='-')return (a-b);
else if(theta=='*')return (a*b);
else if(theta=='/')return (a/b);
}//Operate
Status In(char c){
//判断c是否是运算符,如果是返回OK,否则返回ERROR。
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#')
return OK;
else
return ERROR;
}//In
Status EvaluateExpression(){
//算术表达式求值的算符优先算法。
int a,b,c,x;
char theta;
OPTR=InitStack(); //修改了此处;
Push(OPTR,'#');
OPND=InitStack();c=getchar();
while(c!='#'||GetTop(OPTR)!='#'){
if(!In(c)){
Push((OPND),c);c=getchar();
}
else{
switch(Precede(GetTop(OPTR),c)){
case'<'://栈顶元素优先级低
Push(OPTR,c);c=getchar();break;
case'='://脱括号并接收下一字符
Pop(OPTR,x);c=getchar();break;
case'>'://退栈并将运算结果入栈
Pop(OPTR,theta);
Pop(OPND,b);Pop(OPND,a);
Push(OPND,Operate(a,theta,b));break;
}//switch
}//else
}//while
return GetTop(OPND);
}//EvaluateExpression
int main(){
printf("Input the EvaluateExpression");
printf("The result is: %d",EvaluateExpression());
free(OPTR);free(OPND);
return 0;
}
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct{
int *base;//存储空间初始分配量
int *top;//栈顶指针
int stacksize;//当前已分配的存储空间,以元素为单位
}SqStack;
SqStack *OPTR,*OPND;//全局变量OPTR和OPND分别为运算符栈和运算数栈
//以下InitStack(),Push (),Pop(),Emptys()及GetTop()函数均借鉴数据结构(C语言版)教材
SqStack * InitStack(void){ //*****修改此函数;
//构造一个空栈S
SqStack *S;
S=(SqStack *)malloc(sizeof(SqStack));
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base)exit(OVERFLOW);//存储分配失败
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return S;
}//InitStackz
Status Push (SqStack *S,int e){
//插入元素e为新的栈顶元素
if(S->top-S->base>=S->stacksize){//栈满,追加存储空间
S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base)exit(OVERFLOW);//存储分配失败
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}//if
*S->top++=e;
return OK;
}//Push
Status Pop(SqStack *S,int e){
//若栈不空,则删除S的栈顶元素,用e返回其值
if(S->top==S->base)return ERROR;
e=*--S->top;
return (e);
}//Pop
Status Emptys(SqStack *S){
//若栈空,返回TRUE,否则返回FALSE
if(S->top==S->base)
return TRUE;
else
return FALSE;
}
Status GetTop(SqStack *S){
//若栈不空,则用e返回S的栈顶元素
int e;
if(S->top==S->base)return ERROR;
e=*(S->top-1);
return(e);
}//GetTop
char Precede (char ch1,char ch2){
//构造算符间的优先关系,优先关系借鉴数据结构(C语言版)教材
if(ch1=='+'||ch1=='-')
{
if(ch2=='+'||ch2=='-'||ch2==')'||ch2=='#')return('>');
else if(ch2=='*'||ch2=='/'||ch2=='(')return('<');
}
else if(ch1=='*'||ch1=='/')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2==')'||ch2=='#')return('>');
else if(ch2=='(')return('<');
}
else if(ch1=='(')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='(')return('<');
else if(ch2==')')return('=');
}
else if(ch1==')')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2==')'||ch2=='#')return('>');
//else if()return('=')
}
else if(ch1=='#')
{
if(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='(')return('<');
else if(ch2=='#')return('=');
}
else if(!((ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='('||ch1==')'||ch1=='#')&&(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='('||ch2==')'||ch2=='#')))
//非算符退出
return ERROR;
}
Status Operate(int a,char theta,int b){
//四则运算
if(theta=='+')return (a+b);
else if(theta=='-')return (a-b);
else if(theta=='*')return (a*b);
else if(theta=='/')return (a/b);
}//Operate
Status In(char c){
//判断c是否是运算符,如果是返回OK,否则返回ERROR。
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#')
return OK;
else
return ERROR;
}//In
Status EvaluateExpression(){
//算术表达式求值的算符优先算法。
int a,b,c,x;
char theta;
OPTR=InitStack(); //修改了此处;
Push(OPTR,'#');
OPND=InitStack();c=getchar();
while(c!='#'||GetTop(OPTR)!='#'){
if(!In(c)){
Push((OPND),c);c=getchar();
}
else{
switch(Precede(GetTop(OPTR),c)){
case'<'://栈顶元素优先级低
Push(OPTR,c);c=getchar();break;
case'='://脱括号并接收下一字符
Pop(OPTR,x);c=getchar();break;
case'>'://退栈并将运算结果入栈
Pop(OPTR,theta);
Pop(OPND,b);Pop(OPND,a);
Push(OPND,Operate(a,theta,b));break;
}//switch
}//else
}//while
return GetTop(OPND);
}//EvaluateExpression
int main(){
printf("Input the EvaluateExpression");
printf("The result is: %d",EvaluateExpression());
free(OPTR);free(OPND);
return 0;
}
修改了 InitStack(SqStack *S)函数;出现Segmentation fault错误,是由于Push(OPTR,'#'); OPTR是野指针;
[[it] 本帖最后由 now 于 2008-4-4 13:32 编辑 [/it]]
----------------解决方案--------------------------------------------------------
哦,谢谢!
----------------解决方案--------------------------------------------------------