我正在尝试为C程序实现撤消功能。问题是我收到Debug Assertion Failed(我标记了行)。我是C的初学者,我非常感谢你的帮助。我希望代码是为了解决这个问题。
从Controller.c撤消功能
int undo(controller *ctr) {
if (isEmpty(ctr->operationsStack)){
return 0;
}
Operation operation = pop(ctr->operationsStack); //problem
if (strcmp(getOperationType(&operation), "add") == 0) {
materie mat = getMaterie(&operation);
char nume[8];
strcpy(nume, getNume(&mat));
sterge(ctr->repo, nume);
}
return 1;
}
OperationsStack.h中的结构
typedef struct
{
materie mat;
char operationType[10];
} Operation;
typedef struct
{
Operation operations[100];
int length;
} OperationsStack;
OperationsStack.c
Operation createOperation(materie planet, const char *operationType) {
Operation operation;
operation.mat = planet;
strcpy(operation.operationType, operationType);
return operation;
}
OperationsStack createStack() {
OperationsStack operationsStack;
operationsStack.length = 0;
return operationsStack;
}
void push(OperationsStack *operationsStack, Operation operation) {
if (isFull(operationsStack)) {
return;
}
operationsStack->operations[operationsStack->length++] = operation;
}
Operation pop(OperationsStack *operationsStack) {
if (isEmpty(operationsStack)) {
materie mat; //create a new
mat.cant = 0; //materie object
strcpy(mat.nume, " ");
strcpy(mat.producator, " ");
return createOperation(mat, "none");
}
else
return operationsStack->operations[--operationsStack->length];//problem
}
int isEmpty(OperationsStack *operationsStack) {
return operationsStack->length == 0;
}
我们假设我在我的程序中添加了一个材料类型。这意味着:
答案 0 :(得分:0)
我发现了问题。似乎我在我的程序中有一个中断,这将结束它调用一个函数,它将释放一个未用malloc分配的变量。