首页 > 代码库 > Run-Time Check Failure #2 - Stack around the variable 'usval' was corrupted

Run-Time Check Failure #2 - Stack around the variable 'usval' was corrupted

“val” 为 16进制的字符串,想把字符串每4位的数据提取出来,转成 unsigend short类型,数据已经提取出了,但是在函数调用结束时报错: Run-Time Check Failure #2 - Stack around the variable ‘usval‘ was corrupted。

TCHAR* pMacValue = http://www.mamicode.com/(TCHAR*)(LPCTSTR)val;
TCHAR mac_item[16] = {0};
unsigned short usval = 0;
for(int t = 0;t<tmp->Size;t++)
{    
_tcsncpy(mac_item, pMacValue+t*4, 4);
_stscanf(mac_item, _T("%X"), &usval);
}

 修改类型后,就不在报错了:

TCHAR* pMacValue = http://www.mamicode.com/(TCHAR*)(LPCTSTR)val;
TCHAR mac_item[16] = {0};
unsigned long usval = 0;
for(int t = 0;t<tmp->Size;t++)
{    
_tcsncpy(mac_item, pMacValue+t*4, 4);
_stscanf(mac_item, _T("%X"), &usval);
}

希望专家们看到后能帮忙解答下导致问题产生的原因。

Run-Time Check Failure #2 - Stack around the variable 'usval' was corrupted