Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does memory for localtime need to be deallocated?

Tags:

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?

like image 831
Avihai Marchiano Avatar asked Aug 31 '12 09:08

Avihai Marchiano


People also ask

What happens when memory is not deallocated?

If you lose all pointers to a chunk of memory without deallocating that memory then you have a memory leak. Your program will continue to own that memory, but has no way of ever using it again. A very small memory leak is not a problem.

Which of the following is used to deallocate memory?

Which operator is used to deallocate the memory? Explanation: free operator is used to deallocate the memory.

Which function can be used to deallocate memory when it is no longer needed?

Use the malloc() function to allocate memory in designated blocks and the new function to create memory in the free store (heap). To reallocate memory, the realloc() function is used.


2 Answers

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.

like image 200
Alok Save Avatar answered Oct 14 '22 08:10

Alok Save


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.

like image 24
Rost Avatar answered Oct 14 '22 08:10

Rost