c++ - Why doesn't need to malloc struct tm pointer before call localtime() function? -
my code is
#include <iostream> #include <ctime> using namespace std; void main() { time_t nowtime; struct tm *nowstruct; time(&nowtime); nowstruct = localtime(&nowtime); cout << nowstruct->tm_hour << ":" << nowstruct->tm_min << endl; }
i suspect address of memory used store struct tm.
localtime
uses internal, global buffer (or perhaps thread-local), address returns. practice of keeping global state around similar how strtok
, rand
work. note makes function inherently non-rentrant, , perhaps thread-unsafe.
Comments
Post a Comment