新手问个问题,关于判断栈是否溢出的

bobobe 2014-10-20 12:46:57
顺序存储实现栈的时候

top-base==stacksize可判断栈满

但是如果top指向栈顶下一位置,即指向数组外。top-base==stacksize还可以判断栈满吗,top的指向不明确啊,只有开辟的空间是连续的,才可以通过相减来判断呀。为什么严教材上这样也能判断呢
...全文
301 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-10-21
  • 打赏
  • 举报
回复
Pointer Arithmetic Additive operations involving a pointer and an integer give meaningful results only if the pointer operand addresses an array member and the integer value produces an offset within the bounds of the same array. When the integer value is converted to an address offset, the compiler assumes that only memory positions of the same size lie between the original address and the address plus the offset. This assumption is valid for array members. By definition, an array is a series of values of the same type; its elements reside in contiguous memory locations. However, storage for any types except array elements is not guaranteed to be filled by the same type of identifiers. That is, blanks can appear between memory positions, even positions of the same type. Therefore, the results of adding to or subtracting from the addresses of any values but array elements are undefined. Similarly, when two pointer values are subtracted, the conversion assumes that only values of the same type, with no blanks, lie between the addresses given by the operands.
hslinux 2014-10-21
  • 打赏
  • 举报
回复
顺序存储实现栈,意味着这栈空间是连续分配出来的,top-base就等于栈的目前实际使用容量。 实际使用容量跟栈最大容量相等,不就是栈满了吗?
bobobe 2014-10-20
  • 打赏
  • 举报
回复
引用 1 楼 lovesmiles 的回复:
top和base又不一定是指针。 比如top=10,base=0; 栈的大小是stacksize=10; 那top-base自然就知道是不是满了。 top可能就是一个说明栈顶在第几个位置。所以无须连接空间也可以
我的意思就是指针,不是整型计数器
bobobe 2014-10-20
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
因为top总是每次加1的。
那指针相减到底是什么相减,还是不太明白,希望老师能详细解释一下,谢谢老师
赵4老师 2014-10-20
  • 打赏
  • 举报
回复
因为top总是每次加1的。
勤奋的小游侠 2014-10-20
  • 打赏
  • 举报
回复
top和base又不一定是指针。 比如top=10,base=0; 栈的大小是stacksize=10; 那top-base自然就知道是不是满了。 top可能就是一个说明栈顶在第几个位置。所以无须连接空间也可以

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧