'localtime' 函数在第二次调用时崩溃
'localtime' function crashes on second call
所以,我写了下一个函数:
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...");
}
发布(编辑)的代码没有完全编译
建议始终在启用所有警告的情况下进行编译。
然后您会看到以下两条消息:
...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){
强烈建议在考虑尝试 运行 程序之前解决编译问题。
这会导致未定义的行为:
struct stat *st;
if(stat(route, st) < 0){
您将未初始化的变量 st
传递给库函数,这会导致未定义的行为。
阅读 stat
的 manual page 会告诉您应该提供指向已存在缓冲区的指针,然后 stat
函数将填充。
例如:
struct stat st = { 0 }; // good practice to zero-initialize
if ( stat(route, &st) < 0 ) {
所以,我写了下一个函数:
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...");
}
发布(编辑)的代码没有完全编译
建议始终在启用所有警告的情况下进行编译。
然后您会看到以下两条消息:
...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){
强烈建议在考虑尝试 运行 程序之前解决编译问题。
这会导致未定义的行为:
struct stat *st;
if(stat(route, st) < 0){
您将未初始化的变量 st
传递给库函数,这会导致未定义的行为。
阅读 stat
的 manual page 会告诉您应该提供指向已存在缓冲区的指针,然后 stat
函数将填充。
例如:
struct stat st = { 0 }; // good practice to zero-initialize
if ( stat(route, &st) < 0 ) {