Ver Mensaje Individual
  #2 (permalink)  
Antiguo 16/11/2014, 14:43
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: error "localtime unsafe"

Buenas,

El compilador te dice que el uso de localtime es inseguro, ya que devuelve un puntero a una estructura estática. Esto resulta inseguro ya que esta estructura es única y es muy sencillo pisar el valor sin darse cuenta.
Por ejemplo si hacemos:
struct tm* t1 = localtime(0);
struct tm* t2 = localtime(time());

Tanto t1 como t2, valdrán lo mismo, la hora actual.

En su lugar, el compilador de Microsoft propone la función localtime_s que te permite almacenar el resultado correctamente tu propia estructura.

Código C:
Ver original
  1. private:
  2.  struct tm tiempo;
  3.  int dia;
  4.  int mes;
  5.  int anio;
  6.  
  7.  
  8. /*..................................*/
  9.  
  10.  public:
  11.  string muestraFecha(){
  12.  stringstream s;
  13.  time_t fecha_sistema;
  14.  time(&fecha_sistema);
  15.  localtime_s(&tiempo, &fecha_sistema);
  16.  
  17.  anio=tiempo->tm_year + 1900;
  18.  mes=tiempo->tm_mon + 1;
  19.  dia=tiempo->tm_mday;
  20.  
  21.  s<<dia<<"/"<<mes<<"/"<<anio<<endl;
  22.  return s.str();
  23. }
__________________
If to err is human, then programmers are the most human of us