在尝试打印堆栈的en元素时,堆栈实现崩溃

时间:2014-03-13 15:45:01

标签: c pointers data-structures stack

我正在尝试使用数组实现动态堆栈。用户输入他想要放入堆栈中的单词的字母数。如果堆栈为空,则创建一个块以保存用户传递给堆栈的信息。如果没有,则重新分配堆栈,以便它可以再保存一个信息块(字)。然后用户输入他想要放入堆栈的单词(在打印堆栈索引的部分)。第二个单词进入堆栈后程序似乎崩溃了。

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

typedef struct stackElement {
    int stringLength;
    char *name;
} StackElement;
int Push(StackElement **stack,int *index);

int main()
{
    StackElement *stack = NULL;
    int index = -1;
    Push(&stack,&index);
    printf("The index is : %d\n", index);
    printf("The top word of the stack is %s\n", stack[index].name);
    Push(&stack,&index);
    printf("The index is : %d\n", index);//Crashes after this command is executed 
    printf("The second word of the stack is %s\n", stack[index].name);
    system("PAUSE");
    return 0;
}

int Push(StackElement **stack,int *index)
{
    if (*stack == NULL) {
        printf("The stack is empty\n");
        *index = *index + 1 ;
        *stack = malloc(sizeof(StackElement));
    } else {
        printf("The stack is not empty\n");
        *index = *index + 1 ;
        //Adding enough space for one more element in the stack
        *stack = realloc(*stack,sizeof(StackElement)*(*index+1));
    }
    printf("Enter the length of the word you want in the stack\n");
    scanf("%d", &(*stack[*index]).stringLength );
    (*stack[*index]).name = malloc(sizeof(char)*(*stack[*index]).stringLength );
    printf("Enter the word in the stack\n");
    scanf("%s", (*stack[*index]).name);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

由于指点太多,您的代码不易阅读,但据我了解,您误用了realloc()

首次分配堆栈时,使用效果良好的*stack = malloc(sizeof(StackElement))

当你重新分配堆栈(realloc(*stack,sizeof(StackElement)*(*index)))时,你传递的结构尺寸乘以变量index;在此时程序索引等于1,因此您需要分配与之前完全相同的内存大小(sizeof(StackElement)),然后访问索引大于0的内存,就会出现分段错误。

答案 1 :(得分:0)

这不是很好,但至少它有效!你应该从你的功能中删除你的分配。

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


typedef struct stackElement
{
    int stringLength;
    char *name;

} StackElement;

int Push(StackElement **stack,int *index);



int main()
{
    StackElement *stack = NULL;
    int index = -1;

        if(stack == NULL)
    {
        printf("The stack is empty\n");
        index = index + 1 ;
        stack = (StackElement*)malloc(sizeof(StackElement));
    }
    else
    {
        printf("The stack is not empty\n");
        index = index + 1 ;
        stack = realloc(stack,sizeof(StackElement)*(index+1));//Adding enough space for one more element in the stack
    }

    Push(stack,&index);

    printf("The index is : %d\n", index);

    printf("The top word of the stack is %s\n", stack[index].name);

            if(stack == NULL)
    {
        printf("The stack is empty\n");
        index = index + 1 ;
        stack = (StackElement*)malloc(sizeof(StackElement));
    }
    else
    {
        printf("The stack is not empty\n");
        index = index + 1 ;
        stack = realloc(stack,sizeof(StackElement)*(index+1));//Adding enough space for one more element in the stack
    }


    Push(stack,&index);

    printf("The index is : %d\n", index);//Crashes after this command is executed 

    printf("The second word of the stack is %s\n", stack[index].name);

    system("PAUSE");

    return 0;
}



int Push(StackElement *stack,int *index)
{



    printf("Enter the length of the word you want in the stack\n");

    scanf("%d", &(stack[*index]).stringLength );

    (stack[*index]).name = malloc(sizeof(char)*(stack[*index]).stringLength );

    printf("Enter the word in the stack\n");

    scanf("%s", (stack[*index]).name);




    return 0;

}