From: Martin Bonner on

lists(a)givemefish.com wrote:
> Hi all,
>
> while compiling an existing project in the new MSVC 2005 compiler, I
> received the warning that:
>
> : warning C4996: 'localtime' was declared deprecated
> C:\Program Files\Microsoft Visual Studio
> 8\VC\include\time.inl(114) : see declaration of 'localtime'
> Message: 'This function or variable may be unsafe. Consider
> using localtime_s instead. To disable deprecation, use
> _CRT_SECURE_NO_DEPRECATE. See online help for details.'
>
>
> I wasn't aware that localtime was declared deprecated. Is this true?
Yes. You can see the declaration in the error message. The question
you haven't asked is WHO declared it deprecated. The answer is
Microsoft (because if used carelessly, localtime can overflow a
buffer).
>
> And, if so, is the suggestion of localtime_s standard compliant?
No.

> If not, what should I use? [This program has to be
> platform-independent, ISO C++.]
In that case, you should use localtime (and you probably want to
disable the warning). Of course if you are stuck in one of those
foolish shops that /also/ say "code must compile warning free with all
warnings enabled", you are a bit stuck!


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jake Montgomery on
On 6/3/2006 4:53 AM, kanze wrote:

> lists(a)givemefish.com wrote:
>
>>while compiling an existing project in the new MSVC 2005
>>compiler, I received the warning that:
>
>
>>: warning C4996: 'localtime' was declared deprecated
>>
>>I wasn't aware that localtime was declared deprecated. Is
>>this true?
>
>
> Not by the C++ committee, nor by the C committee (I think).
> Maybe by Microsoft. In fact, the message is misleading; I think
> Microsoft has admitted this, and plans to change it in the next
> release.
>
> On the other hand, there are cases where localetime might be
> unsafe -- it cannot be used in multithreaded code, for example.
> And this fact was recognized and addressed by the C committee.
> Except for the word "deprecated" itself, the Microsoft warning
> pretty much corresponds to what the C committee has said.
>

Could you explain why localtime is considered to be inherently unsafe
for multithreading? It would seem that its thread safety would be
implementation dependent ... especially since AFAIK the C++ standard
does not address threading at all. It would seem that the tm structure
returned would be "per-thread" in any self respecting mutlithreaded
standard C++ library. Is that a naive assumption, or am I missing
something else in the definition that makes it inherently unsafe?

Curious,
Jake Montgomery

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: James Kanze on
Martin Bonner wrote:
> lists(a)givemefish.com wrote:

>> while compiling an existing project in the new MSVC 2005
>> compiler, I received the warning that:

>> : warning C4996: 'localtime' was declared deprecated
>> C:\Program Files\Microsoft Visual Studio
>> 8\VC\include\time.inl(114) : see declaration of 'localtime'
>> Message: 'This function or variable may be unsafe. Consider
>> using localtime_s instead. To disable deprecation, use
>> _CRT_SECURE_NO_DEPRECATE. See online help for details.'

>> I wasn't aware that localtime was declared deprecated. Is
>> this true?

> Yes. You can see the declaration in the error message. 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. On the other hand, there is no way
to use it at all in a multithreaded environment. Which raises
an interesting question: if C++ adopts threading as part of the
language, do we also adopt this TR? Or?

--
James Kanze kanze.james(a)neuf.fr
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! ]

From: James Kanze on
Jake Montgomery wrote:

[...]

> Could you explain why localtime is considered to be inherently
> unsafe for multithreading?

Read the specification. I suppose it could be made thread safe
by using some sort of thread local storage, but in general, the
wording of the standard encourages the use of static data.

> It would seem that its thread safety would be implementation
> dependent ... especially since AFAIK the C++ standard does not
> address threading at all.

The fact that the standard doesn't address threading at all
means that you have undefined behavior as soon as there is more
than one thread.

> It would seem that the tm structure returned would be
> "per-thread" in any self respecting mutlithreaded standard C++
> library.

Well, it's definitely not the case in Posix, which introduced
localtime_r to avoid the problem. It might be the case in some
Windows libraries, however; I'm not sure.

> Is that a naive assumption, or am I missing something else in
> the definition that makes it inherently unsafe?

I think it's a naive assumption, although I can see where it
makes sense. In practice, early threading systems, at least on
Unix systems, didn't have any direct support for thread local
storage, and emulating it was felt to be too expensive, and
possibly to introduce memory leaks -- you allocate the thread
local memory the first time the function is called within a
given thread, but when do you free it?

--
James Kanze kanze.james(a)neuf.fr
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! ]

From: Robert Kindred on

"Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
news:e5mogb$udv$1(a)news.datemas.de...
> lists(a)givemefish.com wrote:
[]
>> And, if so, is the suggestion of localtime_s standard compliant?
>
> No.
>
>> If not, what should I use? [This program has to be
>> platform-independent, ISO C++.]
>
> Keep using 'localtime' until MS comes to its senses (unlikely) or
> until MS removes it completely from its library implementation (also
> unlikely). When/If the latter happens, split the code using an ifdef
> directive.

I am afraid "remove completely" is not so far fetched. I prefer to use
opendir and readdir to scan directories for a modicum of platform
independence. This worked in Borland Builder5, but it is gone from
Microsoft VS2005.

Robert Kindred



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: VC++ compiler bug?
Next: UTF8 and std::string