【发布时间】:2017-03-11 17:02:48
【问题描述】:
我偶然发现了这种行为,想知道这是否是预期的(我觉得不合适)。
我在一个特定的 tm 结构中强制出错,而所有其他的都损坏了。
这是代码(精简到重现问题的最低限度)
int main()
{
cout << "----- Bug test - tm struc -----" << endl;
//--------------------------------------------
//--- Setup struct tm ---
time_t timet_Now = time(NULL);
struct tm* tm1 = localtime(&timet_Now);
struct tm* tm2 = localtime(&timet_Now);
//--- Verify OK - cout shows "28/10/2016"---
cout << tm1->tm_mday << " " << tm1->tm_mon << " " << tm1->tm_year << endl;
cout << tm2->tm_mday << " " << tm2->tm_mon << " " << tm2->tm_year << endl;
// ... so far, so good
// --- Force an error in a different tm struct (xxtm)
time_t xtimet = 1464778020000;
struct tm* xxtm = localtime(&xtimet); //<<< xxtm = null - no runtime error
//--- tm1 and tm2 are now corrupted - cout shows "-1/-1/-1"
cout << tm1->tm_mday << " " << tm1->tm_mon << " " << tm1->tm_year << endl;
cout << tm2->tm_mday << " " << tm2->tm_mon << " " << tm2->tm_year << endl;
//--- This next line crashes the application, as tm1 is corrupted
char* c = asctime(tm1);
return 0;
}
崩溃错误是:MyTest.exe 中 0x0FA520B5 (ucrtbased.dll) 处出现未处理异常:将无效参数传递给认为无效参数致命的函数。
【问题讨论】: