繁体   English   中英

运行时检查失败#2-变量's'周围的堆栈已损坏

[英]Run-Time Check Failure #2 - Stack around the variable 's' was corrupted

我遇到了这个问题:运行时检查失败#2-在Visual Studio 12中损坏了变量's的堆栈。 我也尝试在代码块中,但面临相同的问题。 我也在ideone.com中运行我的代码,它显示运行时错误。 帮帮我 ? 我的代码是:

 #include<iostream> #include<stdio.h> #define MAX 50 using namespace std; typedef struct { long var[20]; long pos; }stack; void init_stack(stack *st) { long i; for(i=0; i<MAX; i++) st->var[i] = -1; st->pos = 0; return ; } void push(stack *st, long item) { if(st->pos == MAX) { printf("stack overflow \\n"); } else st->var[st->pos+1] = item; st->pos++; return ; } void pop(stack *st) { //if(empty(st)) if(st->pos == 0) printf("stack underflow \\n"); else st->var[st->pos] = -1; st->pos--; return ; } long top(stack *st) { long temp; temp = st->var[st->pos]; return temp; } bool empty(stack *st) { if(st->pos==0) return true; else return false; } int main() { stack s; long i, n=9, t; init_stack(&s); printf("STACK PUSH\\n"); for(i=1; i<=n; i++) { push(&s, i); t = top(&s); printf(" %ld\\n", t); } printf("STACK POP\\n"); for(i=1; i<=n; i++) { t = top(&s); printf(" %ld\\n", t); pop(&s); } return 0; } 

您将var声明为包含20元素,但您对其进行了MAX次迭代, MAX定义为50 那可能不是您想要的。 尝试:

long var[MAX];

暂无
暂无

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

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