C程序Debug Assertion失败

时间:2016-03-18 14:57:19

标签: c assertions

我正在尝试为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;
}

我们假设我在我的程序中添加了一个材料类型。这意味着:

  • OperationsStack.length = 1
  • OperationsStack.operations是一种操作类型:
  • OperationsStack.operations [0] .operationType =“add”
  • OperationsStack.operations [0] .mat =(“name1”,“name2”,2(random int number)) 现在我调用undo和operation将得到pop函数的结果,这就是OperationsStack.operations [0]我们有几行。但是这里出现错误。

1 个答案:

答案 0 :(得分:0)

我发现了问题。似乎我在我的程序中有一个中断,这将结束它调用一个函数,它将释放一个未用malloc分配的变量。