472,991 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,991 software developers and data experts.

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 5083
Banfa
9,065 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

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

Similar topics

1
by: Florian Lindner | last post by:
Hello, I want to know if a certain duration is over. I try it like this with timedelta objects: d = datetime.timedelta(minutes = 2) t = time.gmtime() print (t + d < time.gmtime()) gives:
2
by: red floyd | last post by:
Is there a portable way to convert between a struct tm and a time_t, for UTC? I know that time_t is seconds since the epoch (1970-01-01T00:00:00Z). Now, you can convert a time_t to a struct tm...
1
by: KW | last post by:
Hi all, Appreciate if someone can help me out on this. Currently, I have a tm structure holding information of the UTC time, which is very likely to be in the past, meaning not the current...
5
by: Raj | last post by:
Hi I'm very new to C and am trying to get the time a month ago in the format mmyy. What I have tried so far is: char *last_mth; size_t maxsize = 4; struct tm *timeptr = localtime(time(0));
11
by: red floyd | last post by:
I have a copy of the C++ standard. Unfortunately, I don't have a copy of the C89 standard, and the question I have is related to something incorporated by reference. Specifically, what does the...
7
by: Yuanfei | last post by:
Hi There, I just found that there is a problem in vc2005 regarding to time_t and localtime. See code snippets belows. Using this code segment, I found that when ut is 86200, the corresponding...
1
by: OleMacGeezer | last post by:
Hello Everyone, I am a brand new Python programmer with barely a month of experience under my belt. Here are my specs: Mac OSX Panther 10.3.9 Jython 2.1 implementation with Hermes BBS...
16
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want to my code to print the timestamps and each one to include...
1
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want my code to print the timestamps and each one to include...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.