我的堆栈实现有什么问题?

发布于 2025-01-05 09:43:48 字数 611 浏览 3 评论 0原文

我在 C 中创建了一个堆栈的实现。以下是相关的定义/方法,我已经删除了所有错误检查/重新分配,所以不要对此发表评论:

typedef struct {
    Element *data;
    int top;
    int size;
} Stack;

typedef void* Element;

void push(Stack *s, Element e) {
    s->data[(s->top)++] = e;
}

现在在另一个方法中,我有一个循环,我在其中调用 推()。然而

int character;
Stack *s = createStack();
while((character = getchar()) != EOF) {
    int tmp = character;
    push(s, &tmp);  
}

,这并没有按照我想要的方式发挥作用。堆栈每次都接收相同的地址,因此当读取每个字符时,整个堆栈的“内容”都会改变。我如何修改它来执行我想要的操作。例如,当读取abcde时,堆栈看起来像(自上而下)e,d,c,b,a

I created an implementation of a Stack in C. Here are the relevant definitions/methods, I have stripped all the error checking/reallocation so don't comment on that:

typedef struct {
    Element *data;
    int top;
    int size;
} Stack;

typedef void* Element;

void push(Stack *s, Element e) {
    s->data[(s->top)++] = e;
}

Now in another method, I have a loop in which I call push(). Something like

int character;
Stack *s = createStack();
while((character = getchar()) != EOF) {
    int tmp = character;
    push(s, &tmp);  
}

However, this doesn't function how I want it to. The stack receives the same address everytime, thus when each character is read, the "contents" of the entire stack change. How do I modify this to do what I want. e.g when abcde is read, the stack looks like (top-down) e,d,c,b,a.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2025-01-12 09:43:48
int tmp = character;
push(s, &tmp);

在那里,您将局部变量tmp的地址传递给函数push,该函数本身存储指针。每次迭代循环时,变量都会被销毁,并创建另一个局部变量(很可能在旧变量之上),因此您将存储指向已销毁变量的指针。

如果您希望堆栈是通用的并使用 void*,则必须确保您存储的对象的生命周期比堆栈的生命周期长。实现此目的的一种方法是在堆上分配变量:

char* c = malloc(sizeof(char)); // or malloc(1) but sizeof is there in case you
                                // change the type later
*c = character;
push(s, c);

然后在堆栈不再使用后释放每个变量,这样就不会发生内存泄漏。

int tmp = character;
push(s, &tmp);

There you are passing the address of the local variable tmp to the function push which is storing the pointer in itself. Every time the loop is iterated, the variable is destroyed and another local variable is made (most likely on top of the old variable), so you are storing pointers to destroyed variables.

If you want your stack to be generic and work with void*, you'll have to make sure the lifetimes of the objects you store is longer than the lifetime of the stack. One way you can do this is allocate the variables on the heap:

char* c = malloc(sizeof(char)); // or malloc(1) but sizeof is there in case you
                                // change the type later
*c = character;
push(s, c);

Then deallocate each one after the stack is no longer in use so you don't get a memory leak.

云柯 2025-01-12 09:43:48

作为上述答案的附录:

您可以简单地将 int 值转换为指针:

push(s, (int *) tmp);

As an addendum to the above answer:

You can simply cast your int value to a pointer:

push(s, (int *) tmp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文