VS2015编译程序时出错,错误如下:
Run-Time Check Failure #2 - Stack around the variable 'checkCode' was corrupted.
我遇到的情况是,编译如下代码时,出现上诉错误:
// strcpy的字符串溢出.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#pragma warning(disable:4996)
//_CRT_SECURE_NO_WARNINGS
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void func(const char *str)
{
bool checkResult = false;
char checkCode[4];
memset(checkCode, 0, 4);
strcpy(checkCode, str);
if (0 == strcmp("12304", checkCode))
{
checkResult = true;
}
if (checkResult)
{
printf("Check password succeed!\n");
}
else
{
printf("Check password failed!\n");
}
}
int main()
{
func("12304");
system("pause");
return 0;
}
原因分析:
编译器是VS2015的,今天开发项目遇到这个问题,记录下:
编译器报错:Run-Time Check Failure #2 - Stack around the variable 'xxx' was corrupted。
看着字面的意思是:运行时检查失败#2 -变量“xxx”周围的堆栈已损坏。
但是代码的逻辑并无误,很是奇怪,后来网上查了才知道解决方法;
解决方案:
项目---属性---配置属性---C/C++---代码生成---基本运行时检查:
设置为”默认是“,点击应用,确定后即可。
———————————————————————————————————————————
参考链接:https://blog.csdn.net/bigger_belief/article/details/132501110