Re: localtime deprecated?
Jorgen Grahn wrote:
On 3 Jun 2006 20:41:03 -0400, James Kanze <kanze.james@neuf.fr> wrote:
Martin Bonner wrote:
...
[...] The
question you haven't asked is WHO declared it deprecated. The
answer is Microsoft (because if used carelessly, localtime can
overflow a buffer).
Can it? I don't think so.
Me neither.
On the other hand, there is no way
to use it at all in a multithreaded environment.
Am I missing something here? Surely it works if only a single thread
needs to call it, or if I add locking and copying around every call.
And make very sure I don't link against code which uses it.
I'm afraid I grossly overstated my case. It's definitely usable (at
least I think so) if you protect each call with a lock, copying the
results before freeing the lock. And as someone else suggested, an
implementation could even return a pointer to thread specific memory,
eliminating the need for the lock. The first is a lot of extra work
for
the user, however, and the second can result in extremely bad
performance, or maybe even memory leaks, under some systems.
I agree localtime() is broken, but at least it's not as broken as (for
example) gets(), which only works if you know noone will ever feed
your program bad input.
Agreed. There are definitly different degrees of broken; gets() is
at about the highest degree, where as localtime() is considerably lower
(but still needs fixing).
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]