我编写了一个简单的程序来实现基于动态数组的堆栈.realloc用于调整我用来存储堆栈元素的容器的大小.

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

void stack_push(int **stack, int *stackSize, int element);
int stack_pop(int **stack, int *stackSize);

int main()
{
    char ch;
    int *stack = NULL, stackSize = 0;
    
    do
    {
        printf("\n1. Push\n");
        printf("2. Pop\n");
        printf("Exit (0)\n");
        printf("Enter choice : ");
        scanf("%c", &ch);

        switch(ch)
        {
            case '1':
                stack_push(&stack, &stackSize, 1);
                break;
            case '2':
                printf("%d\n", stack_pop(&stack, &stackSize));
                break;
            case '0':
                break;
        }
    } while (ch != '0');

    return 0;
}

void stack_push(int **stack, int *stackSize, int element)
{
    if (!*stack)
    {
        *stack = malloc(sizeof(int));
    }
    else
    {
        *stack = realloc(*stack, sizeof(int) * (*stackSize + 1));
    }

    *stack[*stackSize] = element;
    *stackSize += 1;
}

int stack_pop(int **stack, int *stackSize)
{
    if (!*stack)
    {
        return -1;
    }
    else
    {
        *stackSize -= 1;
        int element = *stack[*stackSize];

        if (*stackSize > 0)
        {
            *stack = realloc(*stack, sizeof(int) * (*stackSize));
        }
        else
        {
            free(*stack);
            *stack = NULL;
        }

        return element;
    }
}

此程序适用于第一个元素.但是,当我try 添加后续元素时,我遇到了分段错误.

我try 调试我的代码,我发现:

线路出现分段故障:

*stack[*stackSize] = element;

以下是显示其他细节的屏幕截图: Segmentation Fault

我哪里错了?

推荐答案

对于初学者来说,您需要编写

scanf(" %c", &ch);

而不是

scanf("%c", &ch);

注意格式字符串中的前导空格.它允许跳过输入缓冲区中的空格字符.

在函数stack_push中,您必须编写:

( *stack )[*stackSize] = element;

而不是

*stack[*stackSize] = element;

因为下标运算符具有比一元运算符*更高的优先级.

The same problem exists in the function stack_pop where 而不是

int element = *stack[*stackSize];

你必须写下:

int element = ( *stack )[*stackSize];

如果堆栈为空,则函数stack_pop返回整数-1时的方法也是如此

int stack_pop(int **stack, int *stackSize)
{
    if (!*stack)
    {
        return -1;
    }
//...

是不好的.通常,-1是可以存储在堆栈中的有效值.

最好像这样声明函数:

int stack_pop(int **stack, int *stackSize, int *element );

也就是说,如果堆栈为空,则函数返回0.否则,它返回一个非零值(例如1),并将指针element指向的变量设置为堆栈中某个元素的值.

或者,您可以再添加一个判断堆栈是否为空的函数.应该在调用函数stack_pop之前调用该函数.

Also it would be also much better if 而不是 the separate variables stack and stackSize you used a structure that contains the corresponding data members as for example:

struct Stack
{
    size_t top;
    int *elements;
};

main中,你可以定义这样一个 struct 的对象:

struct Stack stack = { .top = 0, .elements = NULL };

C++相关问答推荐

无效使用未定义类型'structsquare'?

返回一个包含数组的 struct

C语言中字符数组声明中的标准

为什么复合文字(C99)的返回会生成更多的汇编代码?

C++中矢量类型定义和数据保护的高效解决方案

C中函数类型的前向声明

每个 struct 变量在C中都有自己的命名空间吗?

如果dim指定数组中的数据量,使用dim-1会不会潜在地导致丢失一个元素?

如何使用指向 struct 数组的指针并访问数组中特定索引处的 struct

获取每个循环迭代结束时的当前时间

在句子中转换单词的问题

我的C函数起作用了,但我不确定为什么

在for循环中指向数组开头之前

#定义SSL_CONNECTION_NO_CONST

如何仅使用软件重新初始化STM32微控制器中的USB枚举?

';\n&39;和';\r&39;中的';\n&39;之间有什么关系?

如何在zOS上编译共享C库

与外部SPI闪存通信时是否应禁用中断?

将数组中的所有元素初始化为 struct 中的相同值

GnuCobol 使用 double 类型的参数调用 C 函数