[英]Invalid null pointer error when converting std::chrono::system_clock::time_point::min() to string
I am following an example in Nicolai M. Josuttis' "The C++ Standard Library (Second Edition)", page 152-153, which details an example to print the epoch, current time, minimum and maximum times of the std::chrono::system_clock
introduced in C++11. 我正在Nicolai M. Josuttis的“ C ++标准库(第二版)”中,第152-153页中的示例,该示例详细说明了打印
std::chrono::system_clock
的时代,当前时间,最小和最大时间的示例std::chrono::system_clock
是C ++ 11中引入的。
I am using Microsoft Visual Studio 2012, and get an assertion triggered in <xstring>
, due to an invalid null pointer. 我正在使用Microsoft Visual Studio 2012,并且由于无效的空指针而在
<xstring>
触发了一个断言。 This occurs on the line std::string ts = std::ctime( &t )
in the code below after setting tp = std::chrono::system_clock::time_point::min();
在设置
tp = std::chrono::system_clock::time_point::min();
之后,在以下代码的std::string ts = std::ctime( &t )
行中发生这种情况tp = std::chrono::system_clock::time_point::min();
#include <chrono>
#include <ctime>
#include <string>
#include <iostream>
std::string asString( const std::chrono::system_clock::time_point& tp )
{
std::time_t t = std::chrono::system_clock::to_time_t( tp );
std::string ts = std::ctime( &t );
ts.resize( ts.size()-1 );
return ts;
}
int main()
{
std::chrono::system_clock::time_point tp;
std::cout << "epoch: " << asString(tp) << std::endl;
tp = std::chrono::system_clock::now();
std::cout << "now: " << asString(tp) << std::endl;
tp = std::chrono::system_clock::time_point::min();
std::cout << "min: " << asString(tp) << std::endl;
tp = std::chrono::system_clock::time_point::max();
std::cout << "max: " << asString(tp) << std::endl;
return 0;
}
Is this due to an implementation error by Dinkumware in the <chrono>
library, or just a typo/mistake in the book? 这是由于Dinkumware在
<chrono>
库中的实现错误,还是书中的错字/错误? I have gone over the code given in the book again and again to see if I have copied it out incorrectly, but this does not appear to be the case. 我一次又一次地检查了书中给出的代码,以查看是否将其错误地复制了出来,但事实并非如此。 I'd be very grateful for any insights given.
对于提供的任何见解,我将不胜感激。
It looks like std::ctime
returns NULL, which indicates an incorrect t
value. 看起来
std::ctime
返回NULL,表示不正确的t
值。 Probably because the call to asString
uses a value of time_point
that cannot be represented in time_t
type. 可能是因为对
asString
的调用使用了无法以time_t
类型表示的time_point
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.