Following prototypes apply for localtime and gmtime: - struct tm *localtime(const time_t *timer);
-
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?
3 5207 Banfa 9,065
Recognized Expert Moderator Expert
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.
Thanks again Banfa.
I've changed now my main reference for C language from "http://www.acm.uiuc.ed u/webmonkeys/book/c_guide/" to "http://www.cplusplus.c om/reference/", as I see it is more complete
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
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 for either local (localtime()) or UTC (gmtime()).
But the reverse operation (mktime()) only converts a local time, as far as I can tell. What I'm...
|
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 time.
So, let's say I'm in CST(-6) now, and I want to know the equivalent
local time of that tm structure, how do I do that taking into
consideration...
|
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));
|
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 Standard say about the behavior of gmtime()
when the argument is negative (i.e. before the epoch)?
Is it UB? Should gmtime() return NULL/0? ...
| |
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 output is 1970.1.1 7:56:40, when ut is 0,
the output is 1970.1.1 8:00:00, when ut is 86400, the output is 1970.1.2
8:00:00. While output will be 1970...
|
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 python module installed
|
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 hour:minute:second as of the corresponding old day.
The only routine for this job seems to be
localtime(timestmap) - this is Windows Vista,
Visual Studio...
|
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 hour:minute:second as of the corresponding old day.
The only routine for this job seems to be
localtime(timestmap) - this is Windows Vista,
Visual Studio 2005...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |