0
我遇到了這個問題:運行時檢查失敗#2 - 圍繞變量's'堆棧在Visual Studio 12中損壞。我也在代碼塊中嘗試這個,但面臨同樣的問題。我也在ideone.com上運行我的代碼,它顯示運行時錯誤。幫幫我 ? 我的代碼是:運行時檢查失敗#2 - 圍繞變量's'的堆棧已損壞
#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;
}
MAX是50,你讓VAR是20個元素的數組。 – Infested