QuentinRM
QuentinRM

Reputation: 203

This function or variable may be unsafe visual studio

I got a problem on visual studio. I try to use the localtime function from "time.h".

Visual studio tells me it's an unsafe function. However, I have tu use this one for my school exercice. I saw that you can disable this unsafe error by going in the project properties, build tab, and check "enable unsafe code".

Nevertheless, I don't have a build tab, as you can see there : http://puu.sh/4NkYC.png

I'm using windows 7 and visual studio 2012 Ultimate. It looks like the "build tab" and "enable unsafe code" has vanished :/ Maybe you know how to fix that ?

thank's a lot :)

Upvotes: 11

Views: 68758

Answers (6)

Steve Summit
Steve Summit

Reputation: 48010

The portable, "safe" version of localtime is localtime_r. Like Microsoft's localtime_s mentioned in Henno's answer, localtime_r takes an additional argument which is a pointer to the struct tm to fill in, rather than using a shared, statically-allocated one as localtime does. Example:

time_t t = time(NULL);
struct tm tmbuf;
localtime_r(&t, &tmbuf);

Upvotes: 0

Sreejith Sathyan
Sreejith Sathyan

Reputation: 41

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
char counter_str[10];

int main()
{ 
  time_t my_time = time(NULL)// declaring argument of time();
  sprintf(counter_str,ctime(&my_time));//fetch current time
  printf(counter_str);

}

Upvotes: 4

AlexMelw
AlexMelw

Reputation: 2624

You can switch off the warning using the following directive:

#pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS

Upvotes: 35

Umut D.
Umut D.

Reputation: 1846

Visual Studio (VS) compiler gives this error. It's simple to get rid of this problem.

  1. Go to your VS context menu Project>Properties.
  2. Click Configuration>Properties>C/C++>Preprocessor.
  3. Edit Preprocessor Definitions and add _CRT_SECURE_NO_WARNINGS last empty line.

This compile warning will be gone.

Upvotes: 15

doctorlove
doctorlove

Reputation: 19272

unsafe is part of C# not C++. For example these docs clearly say

/unsafe (C# Compiler Options)

at the top
In C++, visual studio will complain about functions it regards as unsecure and suggest you #define _CRT_SECURE_NO_WARNINGS if you don't want lots of warnings, for example

localtime might give you the following:

warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Upvotes: 4

Henno
Henno

Reputation: 397

localtime is marked unsafe by the MS-Compiler because it returns a pointer to a statically allocated struct tm. This is obviously a bad idea.
Therefore, localtime_s was invented by Microsoft, which takes a pointer to a struct tm allocated by you
struct tm timeinfo;
localtime_s(&timeinfo, &rawtime);

Use this (and have your program be Microsoft specific) or switch off the warning by defining _CRT_SECURE_NO_WARNINGS.

Upvotes: 15

Related Questions