简体   繁体   English

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

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

I have faced this problem : Run-Time Check Failure #2 - Stack around the variable 's' was corrupted in visual studio 12 . 我遇到了这个问题:运行时检查失败#2-在Visual Studio 12中损坏了变量's的堆栈。 I also try this in codeblock but faced same problem . 我也尝试在代码块中,但面临相同的问题。 I run my code also in ideone.com it shows runtime error . 我也在ideone.com中运行我的代码,它显示运行时错误。 Help me out ? 帮帮我 ? my code is : 我的代码是:

 #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; } 

You declare var as holding 20 elements, but you iterate MAX times over it, MAX being defined as 50 . 您将var声明为包含20元素,但您对其进行了MAX次迭代, MAX定义为50 That's probably not what you wanted to do. 那可能不是您想要的。 Try : 尝试:

long var[MAX];

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

相关问题 运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;result&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败#2-变量&#39;NearID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM