stack around the variable "xx" was corrupted

今天碰到这个问题,网上查了下时越界,仔细看代码终于发现问题了:

代码是这样的:

FILE *f = fopen(path, "rb");
if(f)
{
	unsigned short c;
	fread(&c, sizeof(unsigned short), 2, f);
	fclose(f);
}

运行到这里弹框提示“stack around the variable c was corrupted”

问题的原因是往&c这个地址写入了4个字节,而unsigned short大小是2,显然越界了,把2改成1就好了,当然据说把runtimecheck改成default也可以,但是毕竟不太安全,我用的是Both (/RTC1, equiv. to /RTCsu)

你可能感兴趣的:(Runtime,stack,check,check)