Question

i am solving spoj problem where current time stamp is sqaured and then divided by 400000007 and the remainder is converted into a dat time below is the technique i used..

remainder = result%4000000007 ; 

printf("%ul\n",remainder); 


//convert the timestamp into date , time 

dt = localtime(&remainder); 
// use any strftime format spec here 
// strftime(b, sizeof(b), "%m%d%H%M%y", dt); 
// fprintf(stdout, "%s", b); 
// printf("%s\n",asctime(localtime(&remainder))); 
 ltime = (time_t)remainder; 
 printf("%s\n",asctime(dt)); 
 printf("%s\n",ctime(&ltime));

Error shown is Segmentation fault , at asctime , also ltime is time_t variable ,'b' is a buffer , dt is struct tm * , and also i have tried to convert the remainder into time_t variable , but still dint work . Also , Commented code shows my attempts . Using debugger , i found asctime returns null .

where is the problem ?

Was it helpful?

Solution

It is hard to say exactly what is wrong from the code presented. However, in your comments, you related that remainder has the type unsigned long long. However, you pass its address to localtime(), which is an error. localtime() expects a time_t *.

What might have happened is that due to the wrong type being passed in, localtime() detected some error and returned a NULL value. Then, passing this value into asctime() resulted in a NULL return value as well.

As an aside, your printf has the wrong specifier. Use %llu to print an unsigned long long.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top