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

标签 c++ pointers visual-studio-2012 stack runtime-error

我遇到过这个问题:Run-Time Check Failure #2 - Stack around the variable 's' was corrupted in visual studio 12。我也在 codeblock 中尝试了这个,但遇到了同样的问题。我也在 ideone.com 中运行我的代码,它显示运行时错误。帮帮我 ? 我的代码是:

#include
  #include
  #定义最大 50
  使用命名空间标准;
  类型定义结构
  {
    长变量[20];
    长仓;
  }堆;

   void init_stack(堆栈*st)
   {
       长我;
       对于(i=0;ivar[i] = -1;
       st->pos = 0;
     返回 ;
  }

  void push(stack *st, long item)
  {
    如果(st->pos == MAX)
    {
        printf("堆栈溢出\n");
    }
    别的
    st->var[st->pos+1] = 项目;
    st->pos++;
    返回 ;
  }

  无效弹出(堆栈* st)
  {
   //如果(空(st))
    如果(st->pos == 0)
        printf("堆栈下溢\n");
    别的
    st->var[st->pos] = -1;
    st->pos--;
    返回 ;
  }

  长顶(堆叠*st)
  {
    长时间的温度;
    temp = st->var[st->pos];

    返回温度;
    }

 bool 空(堆栈* st)
{
    如果(st->pos==0)
        返回真;
    别的
        返回假;
}

主函数()
{
    堆栈;
    长 i, n=9, t;
    初始化堆栈(&s);
    printf("堆栈推送\n");
    对于(i=1;i<=n;i++)
    {
        推(&s,我);
        t = 顶部(&s);
        printf("%ld\n", t);
    }
    printf("堆叠 POP\n");
    对于(i=1;i<=n;i++)
    {
        t = 顶部(&s);
        printf("%ld\n", t);
        弹出(&s);
    }
    返回 0;
}

最佳答案

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

long var[MAX];

关于c++ - 运行时检查失败 #2 - 变量 's' 周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16891911/

相关文章:

c++ - 如何找到社交网络实体之间的连接类型

android - 用特定值填充所有可用堆栈

c++ - 向双端队列指针发出设置值

c - c中的数组名到底是什么?

c++ - 调试 Win32 API 应用程序是否存在内存泄漏

c++ - Cocos2d-x 3 碰撞

c++ - 基于 CMake 平台的 Clang

c - 删除链表中的重复元素

windows - 无法安装 Visual Studio 2012 Express for Phone 8.0 开发

visual-studio - vstfs ://protocol tries to open in VS 2012 but fails