简体   繁体   English

在cout上打印时间时奇怪的C ++行为

[英]Weird C++ Behavior while printing our time on cout

Running following code: 运行以下代码:

  1 #include <iostream>
  2 int main(int argc, char** argv)
  3 {
  4     time_t t1 = time(0);
  5     time_t t(0);
  6     std::cout<<"BUG LINE"<<std::endl<< ctime(&t) << ctime(&t1) ;
  7     std::cout<<"PRINTS FINE HERE"<<std::endl;
  8     std::cout<< ctime(&t);
  9     std::cout<< ctime(&t1);
 10     return 0;
 11 }

Building: g++ test1.cpp 构建:g ++ test1.cpp

Output:
./a.out
BUG LINE
Thu Jan  1 01:00:00 1970
Thu Jan  1 01:00:00 1970
PRINTS FINE HERE
Thu Jan  1 01:00:00 1970
Wed Jul 10 16:31:48 2013

Why is the stream going weird in the #6 of the code?? 为什么在代码的#6中流变得奇怪?

http://www.cplusplus.com/reference/ctime/ctime/ http://www.cplusplus.com/reference/ctime/ctime/

The returned value points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime . 返回的值指向一个内部数组该数组的有效性或值可通过随后调用asctimectime来更改

http://en.cppreference.com/w/cpp/chrono/c/ctime http://en.cppreference.com/w/cpp/chrono/c/ctime

The string may be shared between std::asctime and std::ctime , and may be overwritten on each invocation of any of those functions . 该字符串可以在std::asctimestd::ctime之间共享,并且在任何这些函数的每次调用时都可能被覆盖

In your example, on line 6, ctime(&t1) gets evaluated first, then ctime(&t) which overwrites the string (whichever is evaluated first is unspecified, and compilers often evaluate in reverse order ( often , not always)). 在您的示例中,在第6行,首先对ctime(&t1)求值,然后覆盖字符串的ctime(&t) (未指定先求值者,并且编译器通常按相反顺序求值( 通常 ,并非总是))。

TL;DR: Reading the docs may help. TL; DR:阅读文档可能会有所帮助。

The function ctime returns a static string buffer - so the string returned is exactly the same string each time), which means that calling it multiple times on the same line will give undefined behaviour (because the C and C++ standards do not tell in what order the calls are made). 函数ctime返回一个static字符串缓冲区-因此每次返回的字符串都是完全相同的字符串),这意味着在同一行上多次调用它会产生未定义的行为(因为C和C ++标准不会以什么顺序告诉拨打电话)。

The solution is to call in two distinct statements (with ; between), or use reentrant-safe variant, where you pass in the buffer to store the result in), eg ctime_r 解决方案是调用两个不同的语句(之间使用;或使用reentrant-safe变体,在该变量中传递缓冲区以将结果存储在其中),例如ctime_r

The problem here is that ctime() uses an internal allocated char array which is modified with each call to ctime() . 这里的问题是ctime()使用内部分配的char数组,每次调用ctime()都会对其进行修改。

The other problem here is that your ctime() s can be evaluated in any order in that expression. 这里的另一个问题是您的ctime()可以在该表达式中以任何顺序求值。 So what is happening here is that both of them on line 6 are evaluated, but clearly the second is evaluated second. 因此,这里发生的是对第6行的两个参数都进行了评估,但是很明显第二个要素被评估了。 Then the operators are applied from left to right, but both calls to ctime have already been evaluated, the left most one was done afterwards and now both strings refer to the same thing. 然后从左到右应用运算符,但已经评估了两次对ctime调用,最左端的调用在此之后完成,现在两个字符串都引用同一件事。

If you placed them on different lines, you would get your expected behaviour. 如果将它们放在不同的行上,则会获得预期的行为。

http://www.cplusplus.com/reference/ctime/ctime/ http://www.cplusplus.com/reference/ctime/ctime/

A C-string containing the date and time information in a human-readable format. 一个C字符串,包含人类可读格式的日期和时间信息。

The returned value points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime. 返回的值指向一个内部数组,该数组的有效性或值可以通过随后对asctime或ctime的任何调用来更改。

The returned value points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime. 返回的值指向一个内部数组,该数组的有效性或值可以通过随后对asctime或ctime的任何调用来更改。

This is exactly what is happening here. 这正是这里发生的事情。 One of the two calls to ctime in that line gets executed before the other, but both get executed before cout can print them. 该行中对ctime的两个调用之一在另一个调用之前执行,但都在cout可以打印它们之前执行。 Since they (probably) return the same temporary char buffer, you get the same result by the time cout gets to the printing. 由于它们(可能)返回相同的临时char缓冲区,因此在cout到达打印时,您将获得相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM