localtime and asctime are unsafe, but the safe functions don't have the same parameters [duplicate]

StackOverflow https://stackoverflow.com/questions/17982366

Domanda

I'm attempting to create timestamps for my program. On my sister's Mac (using Xcode 4.2) this code works perfectly fine:

struct tm * timeInfo;
time_t rawtime;
time (&rawtime);
timeInfo = localtime (&rawtime);
string timestamp(asctime(timeInfo));

However, on my PC running Visual Studio 2012 I get errors for localtime and asctime that tell me they are unsafe functions and it recommends using localtime_s and asctime_s. However, the function parameters are different. I've tried researching the functions as best I can, but I can't get it to work.

Any help with getting that to work would be much appreciated.

edit:

struct tm * timeInfo;
time_t rawtime;
time (&rawtime);
timeInfo = localtime_s (&rawtime);
string timestamp(asctime_s(timeInfo));
È stato utile?

Soluzione

The reason those functions have different parmaters is that the lack of safety is caused by only have a single parameter. In particular, asctime() uses a single buffer to return the time. So if you do something like:

char *s1 = asctime((time_t)0);   // 1-Jan-1970 00:00:00 or something like that. 
time_t t = time();
char *s2 = asctime(t);
cout << "1970: " << s1 << " now:" << s2 << endl;

Then you will not see two different times printed, but the current time printed twice, as both s1 and s2 point to the same string.

The same applies to localtime, which returns a pointer to struct tm - but it's always the same struct tm, so you if you do:

struct tm* t1 = localtime(0);
struct tm* t2 = localtime(time()); 

you will get the same values in t1 and t2 (with the "current" time, not 1970).

So, to fix this problem, asctime_s and localtime_s have an extra parameter that is used to store the data into. asctime_s also have a second extra parameter to tell the function how much space there is in the storage buffer, as otherwise, it could overflow that buffer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top