【问题标题】:'localtime' function crashes on second call'localtime' 函数在第二次调用时崩溃
【发布时间】:2015-10-17 02:01:23
【问题描述】:

所以,我写了下一个函数:

void actualizetime(){
  puts("actualizetime called"); //debugging purposes.
  time_t rawtime;
  puts("static time_t defined"); //debugging purposes.
  struct tm *timeinfo;
  puts("struct tm * defined"); //debugging purposes.
  time(&rawtime);
  puts("time function called"); //debugging purposes.
  timeinfo = localtime(&rawtime);
  puts("timeinfo has localtime"); //debugging purposes.

  actualtime.year = timeinfo->tm_year + 1900;
  actualtime.month = timeinfo->tm_mon;
  actualtime.day = timeinfo->tm_mday;
  actualtime.hour = timeinfo->tm_hour;
  actualtime.min = timeinfo->tm_min;
  actualtime.sec = timeinfo->tm_sec;
}

问题是,这个功能只有一次很好用。第二次调用该函数时,timeinfo = localtime(&rawtime); 崩溃。怎么了?还有,我该如何解决?

我认为actualtime 结构并不重要,但无论如何:

struct {
  int year;
  int month;
  int day;

  int hour;
  int min;
  int sec;
} actualtime;

编辑

完整的程序(仅限 Linux):

警告:要运行这个程序,您必须是 root,并且在 root 文件系统上有一个名为 /pragma/regfiles/ 的目录

#include<sys/stat.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

void actualizetime(void);
void openregfile(void);
void closeregfile(void);
void addreg(char *,char *);
void terminate(void);

char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

FILE *regfile;

struct {
  int year;
  int month;
  int day;

  int hour;
  int min;
  int sec;
} actualtime;

void actualizetime(){
  puts("-actualizetime called"); //debugging purposes.
  time_t rawtime;
  puts("-static time_t defined"); //debugging purposes.
  struct tm *timeinfo;
  puts("-struct tm * defined"); //debugging purposes.
  time(&rawtime);
  puts("-time function called"); //debugging purposes.
  timeinfo = localtime(&rawtime);
  puts("-timeinfo has localtime"); //debugging purposes.

  actualtime.year = timeinfo->tm_year + 1900;
  actualtime.month = timeinfo->tm_mon;
  actualtime.day = timeinfo->tm_mday;
  actualtime.hour = timeinfo->tm_hour;
  actualtime.min = timeinfo->tm_min;
  actualtime.sec = timeinfo->tm_sec;
}

void openregfile(void){
  char route[64];
  struct stat *st;

  if(!route){
    puts("Error initializing files");
    exit(1);
  }

  actualizetime();

  sprintf(route, "/pragma/regfiles/%04d/", actualtime.year);
  if(stat(route, st) < 0){
    if(mkdir(route, 0600) < 0){
      puts("ERROR: cannot create directory for regs files! (1)");
      exit(1);
    }
  }

  sprintf(route, "/pragma/regfiles/%04d/%s", actualtime.year, months[actualtime.month]);
  if(stat(route, st) < 0){
    if(mkdir(route, 0600) < 0){
      puts("ERROR: cannot create directory for regs files! (2)");
      exit(1);
    }
  }

  sprintf(route, "/pragma/regfiles/%04d/%s/%02d.reg", actualtime.year, months[actualtime.month], actualtime.day);
  if((regfile = fopen(route, "a")) == NULL){
    puts("ERROR: cannot create reg file!");
    exit(1);
  }
  addreg("log", "reg start");
}

void closeregfile(){
  addreg("log", "reg stop");
  fclose(regfile);
}

void addreg(char *label,char *msg){
  puts("-before actualize time");  //debugging purposes.
  actualizetime();
  puts("-after actualize time");  //debugging purposes.
  printf("%d%d%d", actualtime.hour, actualtime.min, actualtime.sec);
  fprintf(regfile, "[%s]:%04d%s%02d_%02d%02d%02d %s\n", label, actualtime.year, months[actualtime.month], actualtime.day, actualtime.hour, actualtime.min, actualtime.sec, msg);
  fflush(regfile);
}






int main(void){
  openregfile();

  //Do something

  terminate();
  return 0;
}

void terminate(void){
  closeregfile();
  puts("Terminating...");
}

【问题讨论】:

  • 请展示一个最小的、完整的程序。我没有看到任何内存/指针问题,因此无法从这段代码 sn-p 确定其他事情。
  • @ydobonebi: localtime 尚未被 C 标准弃用。 Microsoft 可能它已被弃用,但 Microsoft 不控制 C(甚至不实施当前标准或之前的标准)。
  • @KeithThompson 我想我真的应该停止信任微软的另一个原因:)
  • @ydobonebi:C11 确实添加了 localtime_s 函数。附件K中规定,可选;实现可以预定义__STDC_LIB_EXT1__ 以表明它符合它。
  • 鉴于显示的代码是“安全的”并且您不是在使用多线程程序,因此您必须有其他一些内存滥用,可能在与此代码大部分或完全无关的区域.如果可能的话,你应该看看在valgrind 下运行你的代码——或者使用其他一些内存监视器——看看这是否有问题。

标签: c localtime


【解决方案1】:

发布(编辑)的代码没有完全编译

建议始终在启用所有警告的情况下进行编译。

然后您会看到以下两条消息:

...c:49:6 warning: the addres of 'route' will always be evaluated as 'true' [-Waddress]

if(!route){

...c:57:10: warning 'st' is used unitialized in this function [-Wuninitalized]

if(stat(route, st) < 0){

强烈建议在考虑尝试运行程序之前修复编译问题。

【讨论】:

  • 谢谢,我会的。你如何得到所有这些警告?我使用“pedantic”标志运行 gcc,但看不到您发布的内容。
  • 至少:'gcc -Wall -Wextra -pedantic -std=c99 -c' '-c' 是为了调用编译步骤而不是链接步骤。
  • 在函数中:openregfile(),栈数组'char route[64]'在没有被初始化的行中被使用:'if(!route){'强烈建议删除整个'if ' 代码块
  • 在创建目录时,例如使用 mkdir,需要包含执行权限和以后访问读取和写入权限的权限。
  • 在调用系统函数后显示错误的正确方法是不使用 puts() 或 printf()。而是使用 perror()。 perror 将输出您插入的文本并输出适当的(给定 errno 值)系统错误消息
【解决方案2】:

这会导致未定义的行为:

struct stat *st;
if(stat(route, st) < 0){

您将未初始化的变量 st 传递给库函数,这会导致未定义的行为。

读取manual page 中的stat 会告诉您,您应该提供一个指向已经存在的缓冲区的指针,而stat 函数将填充。

例如:

struct stat st = { 0 };     // good practice to zero-initialize
if ( stat(route, &st) < 0 ) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多