gpt4 book ai didi

c - 使用堆栈或 atoi() 时出现运行时错误

转载 作者:行者123 更新时间:2023-11-30 17:12:29 28 4
gpt4 key购买 nike

#define MAXL 256

第一遍:in = "25 7 * 14 - 6 +"; 顺利运行,答案正确。

第二遍:in = "1 24 3 + * 41 -"; 程序在输出 Num got in: 41 后立即停止 预期:继续循环并进入减号,然后 pop(s) 并减去 41

我猜测我的程序耗尽了分配的空间,因为 free() 没有按照我的预期完成工作,但我不确定。

任何帮助将不胜感激。谢谢!

double evaluatePost(char * in)
{
double * op1 = NULL, * op2 = NULL, * msgr = NULL;
int i, j;
char * c = {0}, tempExp[MAXL] = {0};
char ** token = NULL;
Stack * s = createStack();

strcpy(tempExp, in); /* Copy in to a temporary array so strtok will not destroy in */

for(c = strtok(tempExp, " "); c != NULL; ++i, c = strtok(NULL, " "))
{
if(isdigit(c[0]))
{
printf("\nNum got in: %s\n", c); /* Crash right after this line output 41 */
system("PAUSE");
msgr = (double*)malloc(sizeof(double)); /* I made a malloc check here, it never showed error */
*msgr = atoi(c); /* I don't know if it crash at this line or the next one */
push(s, msgr); /* stack has no limit, receives stack* and void* */
/* It never got pass to here after output 41 */
}
else
{
op2 = (double *)pop(s);
op1 = (double *)pop(s);
printf("\n%f %f %s\n", *op1, *op2, c);
system("PAUSE");
msgr = (double*)malloc(sizeof(double));
if(!msgr)
{
printf("Memory allocation failed.\n");
system("PAUSE");
exit(1);
}
switch(*c)
{
case '+': *msgr = (*op1 + *op2); break;
case '-': *msgr = (*op1 - *op2); break;
case '*': *msgr = (*op1 * *op2); break;
case '/': *msgr = (*op1 / *op2); break;
}
printf("\n%.1f\n", *msgr);
system("PAUSE");
/* Free the memory before they become orphans */
free(op1), free(op2);
push(s, msgr);
}
}
returnVal = *((double *)pop(s));
makeEmpty(s);

return returnVal;
}
void push(Stack * stack, void * dataInPtr)
{
/* Define a new StackNode */
StackNode * newPtr;

/* Get some Memory */
newPtr = (StackNode*)malloc(sizeof(StackNode));
if(!newPtr)
{
printf("Out of memory");
system("PAUSE");
exit(1);
}
/* Assign dataIn to dataPtr */
newPtr->dataPtr = dataInPtr;

/* Make the links */
newPtr->link = stack->top; /* Point both to top */
stack->top = newPtr; /* newPtr at top pointed to be head */
(stack->count)++;
}
void * pop(Stack * stack)
{
/* Hold the data */
void * dataOutPtr;
StackNode * temp;

/* Check if stack is empty */
if(stack->count == 0)
dataOutPtr = NULL;
else
{
/* Get the data and remove the node */
temp = stack->top; /* temp points to top */
dataOutPtr = stack->top->dataPtr; /* dataOutPtr has data */
stack->top = stack->top->link; /* stack moves to next node */
temp->link = NULL; /* break top node off stack */
free(temp); /* frees memory */
(stack->count)--;
}
return dataOutPtr;
}
typedef struct node
{
void * dataPtr;
struct Node * link;
} StackNode;

typedef struct
{
int count;
StackNode * top;
} Stack;

最佳答案

您的问题可能出在这里:

您正在 pop() 中释放 Stack 节点

temp = stack->top;                  /* temp points to top */
dataOutPtr = stack->top->dataPtr; /* dataOutPtr has data */
stack->top = stack->top->link; /* stack moves to next node */
temp->link = NULL; /* break top node off stack */
free(temp); /* frees memory */

之后,您将在 evaluatePost() 中释放节点中的数据指针:

free(op1), free(op2);

释放内存的顺序应始终与malloc相反,即您应先分配 Node,然后分配节点数据,然后再分配 free > 首先释放节点数据,然后释放节点本身。

您遇到的是某些输入的问题,而不是其他输入的问题,因为这是未定义的行为

要了解更多关于C中未定义行为的信息,可以在SO中搜索相关问题。

关于c - 使用堆栈或 atoi() 时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31499104/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com