468,251 Members | 1,365 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,251 developers. It's quick & easy.

gmtime and localtime functions from time.h

Following prototypes apply for localtime and gmtime:

Expand|Select|Wrap|Line Numbers
  1. struct tm *localtime(const time_t *timer);
  2. struct tm *gmtime(const time_t *timer);  
I wonder, should I understand that localtime and gmtime do allocate a "struct tm" element, and so I must do a free() on them after using the value?

If they do not allocate it, where does its memory come from?
Sep 18 '08 #1
3 4565
Banfa
9,033 Expert Mod 8TB
No the memory is not allocated, a simple google search on gmtime will get you to help on this function that explains

Return Value
A pointer to a tm structure with the time information filled in.

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the contents of this structure is overwritten.
What this is saying is that the library contains a single statically allocated structure for use by both functions, that is the memory is permanently allocated.

This has the following implications
  • There is no need to call free on the returned point. In fact calling free is likely to cause a memory exception since you will be trying to free memory that has not been allocated using malloc.
  • If you are intending to make several calls to gmtime or localtime before using the results then you can not just store the pointer each time because they will all end up pointing to the same location and you will only get 1 result. After each call to gmtime/localtime you would need to copy the structure pointed to to localstorage of your own.
  • The functions gmtime/localtime are not re-entrant. That is in a multi-tasking program making a call in one task to either function while another task is already in a call to either function will produce unpredictable results. In a multi-tasking environment calls to these functions must be protected so that only 1 call at a time may be in progress.
Sep 18 '08 #2
Thanks again Banfa.

I've changed now my main reference for C language from "http://www.acm.uiuc.edu/webmonkeys/book/c_guide/" to "http://www.cplusplus.com/reference/", as I see it is more complete
Sep 18 '08 #3
By the way,

Thanks for your comment:

# The functions gmtime/localtime are not re-entrant. That is in a multi-tasking program making a call in one task to either function while another task is already in a call to either function will produce unpredictable results. In a multi-tasking environment calls to these functions must be protected so that only 1 call at a time may be in progress
It was a critical issue I wouldn't have been aware myself. Now I'll use localtime_r() which my system implements.
Sep 18 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Florian Lindner | last post: by
2 posts views Thread by red floyd | last post: by
5 posts views Thread by Raj | last post: by
11 posts views Thread by red floyd | last post: by
1 post views Thread by OleMacGeezer | last post: by
16 posts views Thread by maruk2 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.