gettimeofday에서 오류 ㅡㅡ;

justin의 이미지

msec까지 잘(?) 나오는 군요.

가끔가다 이전값보다 작은 값이 나오는게 무슨 이유일까요?

// gettime.c-----------------------------------------------------------------------
#include <sys/time.h>

long gettime()
{
long msec;
int errno;
struct timeval tv;
if(gettimeofday(&tv, 0)<0) {
printf("gettimeofday() : error %s\n", strerror(errno));
// exit(1);
return -1;
}
msec = tv.tv_usec/1000;
msec += tv.tv_sec*1000;
if( (msec%(10*60*1000)) == -1) return 0;
else return (msec%(10*60*1000));
}

int main()
{
int n=0;
long now, prev;
while(n<6){
now = gettime();
if(now != prev) {
if(now == 11) n++;
printf("n(%d)\t%ld", n, now);
if(prev>now) {

printf("\t\tprev : %ld", prev);
printf("\t now : %ld\n", now);
}
else {
printf("\n");
}
}
prev = now;
}

return 0;
}
//------------------------------------------------------------------------------------

아래는 실행 결과 입니다.
n(0) 419949
n(0) 419950
n(0) 419948 prev : 419950 now : 419948
n(0) 419949
n(0) 419951
n(0) 419952
n(0) 419953
n(0) 419954
n(0) 419955
n(0) 419956
n(0) 419957
n(0) 419958
n(0) 419959
n(0) 419960
n(0) 419961
n(0) 419962
n(0) 419964
n(0) 419963 prev : 419964 now : 419963
n(0) 419964
n(0) 419965

mach의 이미지

overflow군요.
현재 시간 2004년 6월 18일 16시 5분 경을 초로 환산하면 1,087,542,345 인데요(글쓰는 시점)

justin wrote:

long msec; // 32비트, signed인 경우 최대 2Giga(2,147,483,647)가능
msec += tv.tv_sec*1000;

여기에 곱하기 1000을 하면 1,087,542,345,000인데, 이 숫자는 long에서는 수용불가합니다.
따라서 뺑뺑이(오버플로우로 인해... % 2,147,483,647를 해보면 대충 나올것임)를 돌다보면 묘한 값이 나오게 됩니다.
오버플로우에 대해 공부하세요.

해법은 여러가지 방법이 있는데, 64비트 정수를 사용하는 사례는 대충 아래와 같으리라고 봅니다.
참고하시길

justin wrote:

// 소스를 대체로 생략했고, 주요 코드만 썼음
long long gettime()
{
long long msec;
...
msec = tv.tv_usec/1000;
msec += tv.tv_sec*(long long)1000;
if( (msec%(10*60*1000)) == -1) return 0;
else return (msec%(10*60*1000));
}

int main()
{
long long now, prev;
...
printf("n(%d)\t%lld", n, now);

printf("\t\tprev : %lld", prev);
printf("\t now : %lld\n", now);

}

------------------ P.S. --------------
지식은 오픈해서 검증받아야 산지식이된다고 동네 아저씨가 그러더라.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.