Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
堆栈实现中的EXC_错误访问_C_Xcode_Xcode5_Exc Bad Access - Fatal编程技术网

堆栈实现中的EXC_错误访问

堆栈实现中的EXC_错误访问,c,xcode,xcode5,exc-bad-access,C,Xcode,Xcode5,Exc Bad Access,我试图编写一个程序来检查平衡括号,但是当我试图初始化堆栈/数组时,我得到了一个错误的访问(代码=1,地址0x0) 代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #define TRUE 1 #define FALSE 0 #define MAX_STRING_LEN 1000 #define MAX_STACK_SIZE 5000 typedef char StackEl

我试图编写一个程序来检查平衡括号,但是当我试图初始化堆栈/数组时,我得到了一个错误的访问(代码=1,地址0x0)

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

#define MAX_STRING_LEN 1000
#define MAX_STACK_SIZE 5000

typedef char StackElement;

typedef struct Stack{
    StackElement *content;
    int maxSize;
    int top;
}Stack;


void Init(Stack*stack);
void Destroy(Stack*stack);
void Push(Stack*stack, StackElement element);
StackElement Pop(Stack*stack);  
int IsEmpty(Stack*stack);
int IsFull(Stack*stack);



/*
 * A new stack variable is initialized.  The initialized
 * stack is made empty. MaxSize is used to determine the
 * maximum number of character that can be held in the
 * stack.
 */
void Init(Stack *stack)
{
    StackElement *newContent;

    /* Allocate a new array to hold the content.*/
    newContent = (StackElement*)malloc(sizeof(StackElement)* MAX_STACK_SIZE);

    if (newContent == NULL) {
        printf("Insufficient memory to initialize stack.\n");
        exit(1);/* Exit, returning error code.*/
    }

    stack->content = newContent;
    stack->maxSize = MAX_STACK_SIZE;
    stack->top = -1;
}

int CheckBalancedParentheses (char* expression){
    int i = 0;
    Stack *stack;
    Init(stack);

    if (strcmp(&expression[i], ")") == 0 || strcmp(&expression[i], "]") == 0 || strcmp(&expression[i], "}") == 0){
        return FALSE;
    }

    for (i = 0; i < strlen(expression); i++){
        if (strcmp(&expression[i], "(") == 0 || strcmp(&expression[i], "[") == 0 || strcmp(&expression[i], "{") == 0){
            Push(stack, expression[i]);
        }
    }

    return TRUE;
}
我不明白为什么它给了我一个糟糕的访问,任何帮助/建议将不胜感激


高级版谢谢。

您已经定义了堆栈指针,这不会分配内存

在调用Init之前,需要在CheckBalancedParenthes中使用malloc语句:

/* Allocate memory for our stack */
Stack* stack = malloc( sizeOf(Stack) );

stack->content
stack
为空。@BLUEPIXY
stack
未初始化。@AlexD未确认。我试图使用Init初始化它,并使用stack*stack声明它。Init函数正在初始化它,或者至少我认为是这样。@pmtry
Stack;初始化(&堆栈)
Stack*Stack=malloc(sizeof(*Stack));初始化(堆栈)
/* Allocate memory for our stack */
Stack* stack = malloc( sizeOf(Stack) );