揮発性のメモ2

http://d.hatena.ne.jp/iww/

time()がエラーを返すことはあるか

エラー
EFAULT
t がアクセス可能なアドレス空間の外を指している。

Manpage of TIME
int main()
{
    time_t t;
    t=time((void*)1);
    printf("%d\n",t);
}
$ ./a.out
セグメンテーション違反です

普通にセグメンテーション違反です
エラーどころじゃない


libcのsysdeps/unix/time.c に定義されてるっぽい

time_t
time (t)
     time_t *t;
{
  struct timeval tv;
  time_t result;

  if (__gettimeofday (&tv, (struct timezone *) NULL))
    result = (time_t) -1;
  else
    result = (time_t) tv.tv_sec;

  if (t != NULL)
    *t = result;
  return result;
}

まあこの期に及んで このコードで__gettimeofday()EFAULTを返すことも無いだろうし、これ以上はどうでもいい