首先,我查看了其他类似的问题但他们没有帮助我。我有以下Valgrind输出:
我的代码:
int f_gozlemZamaniFarki(long long t1,long long t2)
{
char *time1 = (char*)calloc(12,sizeof(char));
char *time2 = (char*)calloc(12,sizeof(char));
*time1 = '\0';
*time2 = '\0';
sprintf(time1,"%lli",t1);
sprintf(time2,"%lli",t2);
int year,month,day,hour,minute,timeDif;
int status = 0;
struct tm tm1;
struct tm tm2;
time_t timeF = NULL;
time_t timeS = NULL;
if(sscanf(time1,"%4d%2d%2d%2d%2d",&year,&month,&day,&hour,&minute) != EOF)
{
tm1.tm_year = year - 1900;
tm1.tm_mon = month;
tm1.tm_mdmonth = day;
tm1.tm_hour = hour;
tm1.tm_min = minute;
tm1.tm_sec = 0;
timeF = mktime(&tm1);
}
if(sscanf(time2,"%4d%2d%2d%2d%2d",&year,&month,&day,&hour,&minute) != EOF)
{
tm2.tm_year = year - 1900;
tm2.tm_mon = month;
tm2.tm_mdmonth = day;
tm2.tm_hour = hour;
tm2.tm_min = minute;
tm2.tm_sec = 0;
timeS = mktime(&tm2);
}
timeDif = (timeF - timeS) / 60;
if ((timeDif < 0) || (timeDif > 59))
{
status = 1;
}
free(time1);
free(time2);
return status;
}
valngrid输出
==12726== Invalid read of size 1
==12726== Address 0x1786dccc is 0 bytes after a block of size 12 alloc'd
==12726== at 0x4C2AE45: calloc (vg_replace_malloc.c:711)
==12726== by 0x405171: f_gozlemZamaniFarki (in /home/aws/bin/awsdDataWriter)
==12726== Conditional jump or move depends on uninitialised value(s)
==12726== Uninitialised value was created by a stack allocation
==12726== at 0x405150: f_gozlemZamaniFarki (in /home/aws/bin/awsdDataWriter)
我的所有变量都被初始化(我希望),所以我不明白为什么Valgrind会对我大喊大叫。 您是否有建议使代码正确并犯错误?
答案 0 :(得分:0)
我改变了这些线条,这解决了我的问题。
char *time1 = (char*)calloc(13,sizeof(char));
char *time1 = (char*)calloc(13,sizeof(char));
//*time1 = '\0';
//*time2 = '\0';