简体   繁体   English

stack_overflow()函数如何工作?

[英]How does the stack_overflow() function work?

So I just came to the part in my book where stacks are explained. 因此,我刚进入书中解释堆栈的部分。 Here's how they get defined in my book (code): 在我的书(代码)中定义它们的方式如下:

#include <stdbool.h>

#define STACK_SIZE 100

int contents[STAC_SIZE]
int top = 0;

void make_empty(void){
    top = 0;
}

bool is_empty(void){
    return top == 0;
}

bool is_full(void){
    return top == STACK_SIZE;
}

void push(int i){
    if(is_full()){
        stack_overflow();
    }
    else{
        contents[top++] = I;
    }

int pop(void){
    if(is_empty()){
        stack_underflow();
    }
    else{
        return contents[--top];
    }
}

I don't get 2 things here: 我在这里没有两件事:

  • how can the make_empty() function work? make_empty()函数如何工作? By just initializing top to 0, contents has still all its elements loaded in... shouldn't contents be initialized as well? 通过仅将top初始化为0, contents仍然加载了所有元素... contents应该初始化?
  • what do the 2 stack_overflow() and stack_underflow() functions do? 2个stack_overflow()stack_underflow()函数有什么作用? Is their behavior defined by default in stdio.h ? 它们的行为是否默认在stdio.h定义? If so, what happens when they get called? 如果是这样,当他们接到电话时会发生什么? If not, am I supposed to define them by myself? 如果没有,我应该自己定义它们吗?

Also, just out of curiosity, is there a library that defines all the stack functions already? 另外,出于好奇,是否有一个库已经定义了所有堆栈函数?

To answer your questions in order: 要按顺序回答您的问题:

  1. make_empty() works like that because the push() function puts an element at the current top of the stack. make_empty()之所以这样工作,是因为push()函数将元素放在堆栈的当前top Yes, contents still has the old values from the stack, however this is not important because they will be overwritten. 是的, contents仍然具有来自堆栈的旧值,但这并不重要,因为它们将被覆盖。

  2. I think that stack_overflow() and stack_underflow() are put there just to illustrate what the normal behavior is when a push is made on a full stack, or a pop on an empty stack. 我认为将stack_overflow()stack_underflow()放在此处只是为了说明在对整个堆栈进行推送或对空堆栈进行弹出操作时的正常行为。 They are not implemented in stdio.h. 它们未在stdio.h中实现。 If you really want, you can create them yourself and include a printf, such as 如果确实需要,可以自己创建它们,并包含一个printf,例如

     void stack_overflow() { printf("Stack Overflow! The program will crash!"); exit(1); //this function exits the program. It is defined in stdlib.h } 

The top variable tells the implementation where the top of the stack is, or, equivalently, how many elements it contains. top变量告诉实现,堆栈的顶部在哪里,或者等效地,它包含多少个元素。 That speaks to the logical contents, not the physical memory the stack occupies. 这说明逻辑内容,而不是堆栈占用的物理内存。 Because this implementation uses an array to store the stack elements, every available position will always have some value stored in it. 因为此实现使用数组存储堆栈元素,所以每个可用位置将始终存储一些值。 The key question is which correspond to bona fide stack elements , and that's exactly what top describes. 关键问题是真正的堆栈元素 相对应 ,而这正是top所描述的。 Thus, make_empty() works by adjusting top to say that the stack storage contains no actual stack elements. 因此, make_empty()通过调整top来说堆栈存储不包含实际的堆栈元素而起作用。 The whole storage is available for elements. 整个存储可用于元素。

The stack_overflow() and stack_underflow() are not standard functions. stack_overflow()stack_underflow()不是标准函数。 The names appear to be intended to convey the idea that a stack overflow or stack underflow occurs in that situation, without specifying any particular behavior. 名称似乎旨在传达在这种情况下发生堆栈溢出或堆栈下溢的想法,而没有指定任何特定行为。

A real-world implementation might implement those to emit a diagnostic message, return an error code (if the implementation provided for that, which the example doesn't), terminate, or, if it were a bit shoddy, commence undefined behavior in those cases. 现实世界中的实现可能会实现那些功能以发出诊断消息,返回错误代码(如果为示例提供了实现,但示例中没有提供),终止,或者如果有点伪劣,则在这些行为中开始未定义的行为案件。 In the overflow case, there might also be the potential to expand the stack, but the details of your particular example do not make that a possibility. 在溢出的情况下,也可能会扩展堆栈,但是您的特定示例的详细信息并没有提供这种可能性。

how can the make_empty() function work? make_empty()函数如何工作?

Think about it this way: What effect do you expect make_empty to have? 这样考虑:您期望make_empty有什么作用? Well, to empty the stack obviously, but what does that mean in terms of the interface we have? 好吧,显然要清空堆栈,但是就我们拥有的接口而言,这意味着什么呢? A couple of things: 有两件事:

  • After emptying the stack, is_empty should be true. 清空堆栈后, is_empty应该为true。 Does setting top = 0 accomplish that? 设置top = 0可以做到吗? Yes, because is_empty just checks that top == 0 . 是的,因为is_empty只检查top == 0
  • After emptying the stack, popping it should cause an underflow. 清空堆栈后,将其弹出会导致下溢。 Does setting top = 0 accomplish that? 设置top = 0可以做到吗? Yes, because pop will cause an underflow if is_empty is true, which it will be. 是的,因为如果is_empty为true, pop会导致下溢。
  • After emptying the stack, we should be able to push STACK_SIZE times before we get an overflow. 清空堆栈后,我们应该能够在出现溢出之前压入STACK_SIZE次。 Does setting top = 0 accomplish that? 设置top = 0可以做到吗? Yes, because push increases top by one each time and won't cause an overflow until top == STACK_SIZE . 是的,因为push每次都会使top每次增加一,并且直到top == STACK_SIZE引起溢出。 So if top is zero, we can push STACK_SIZE times. 因此,如果top为零,我们可以按STACK_SIZE次。

By just initializing top to 0, contents has still all its elements loaded in... shouldn't contents be initialized as well? 通过仅将top初始化为0,内容仍然加载了所有元素...内容是否也应该初始化?

To what? 要什么? An array of size 0? 大小为0的数组? We can't do that because contents has been declared to have size STACK_SIZE , not 0. An array's size is fixed and can never change. 我们不能这样做,因为已声明contents大小为STACK_SIZE ,而不是0。数组的大小是固定的,并且永远不会改变。

We could set all of the elements to 0, but that wouldn't actually accomplish anything. 我们可以将所有元素都设置为0,但这实际上不会完成任何事情。 The values of the elements with index >= top will never affect anything, so why bother changing them? 索引>= top的元素的值永远不会影响任何东西,那么为什么要麻烦更改它们呢?

what do the 2 stack_overflow() and stack_underflow() functions do? 2个stack_overflow()和stack_underflow()函数有什么作用?

Presumably they're supposed to tell the user that an over- or underflow has occurred and then exit the application or invoke some sort of error handler. 大概他们应该告诉用户发生了上溢或下溢,然后退出应用程序或调用某种错误处理程序。

Is their behavior defined by default in stdio.h? 是否在stdio.h中默认定义了它们的行为?

No. 没有。

If not, am I supposed to define them by myself? 如果没有,我应该自己定义它们吗?

Presumably. 想必。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM