24
void log(){
    time_t current = time(0);
    tm *ptm = localtime(&current);
        stuf...
}

Just want to be sure - do i need to release tm pointer allocated memory at the end of the method?

jww
  • 97,681
  • 90
  • 411
  • 885
Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
  • possible duplicate of [How is the result struct of localtime allocated in C?](http://stackoverflow.com/questions/8694365/how-is-the-result-struct-of-localtime-allocated-in-c) – user Oct 19 '14 at 08:30
  • Also see [How to resolve dyld and localtime leaks when profiling a project?](http://stackoverflow.com/q/10105181) – jww Oct 02 '15 at 16:25

2 Answers2

27

No you should not deallocate it,the structure is statically allocated.

Check the documentation:

Return value
pointer to a static internal std::tm object on success, or NULL otherwise. The structure may be shared between std::gmtime, std::localtime, and std::ctime, and may be overwritten on each invocation.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
8

No, you shouldn't. This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.

So be careful with results - e.g. copy them immediately and don't store the pointer.

Rost
  • 8,779
  • 28
  • 50