473,414 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 5175
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.