变量周围的VS报告堆栈已损坏

VS reporting stack around the variable was corrupted

本文关键字:堆栈 已损坏 报告 VS 周围 变量      更新时间:2023-10-16

我的平台是Windows 7 x64。此代码:

#include <iostream>
#include <string>
#include <stdint.h>
#include <sstream>
using namespace std;
int main()
{
    string line;
    int c = 3;
    while(c-- && getline(cin, line))
    {
        stringstream sstr;
        sstr << line;
        uint32_t data;
        int r = sscanf(sstr.str().c_str(), "%d", &data);
        if (r == 1)
        {
            cout << data << endl;
        }
    }
    return 0;
}

将整数作为输入,打印它们,并正确存在。如果我将data的类型更改为uint16_t,那么就在从main((函数返回之前,VS 2010抛出一个错误:

运行时检查失败#2-变量"数据"周围的堆栈已损坏

我想知道为什么VS 2010对uint16_t如此愤怒?有什么解释吗?

可能是因为%d引用了一个32位的值。对于16位值,请尝试%hd