簡體   English   中英

使用time.h時的Segfault

[英]Segfault when using time.h

好吧,我一直在嘗試我所知道的所有事情,讓這個程序停止崩潰,但我不明白為什么。 我能夠將問題與ctime代碼隔離開來,並且只是做了一個小程序來演示出錯了什么。 此代碼編譯沒有問題。

#include<iostream>
#include<ctime>

int main();
time_t getDay(time_t t);
int diffDay(time_t end,time_t begin);

int main()
{
    time_t curTime=time(NULL);  //Assign current time
    time_t curDay=getDay(curTime);  //Assign beginning of day
    time_t yesterday=curDay-16*60*60;   //Assign a time that's within yesterday
    time_t dif=diffDay(curTime,yesterday); //Assign how many days are between yesterday and curTime


    std::cout << "Cur Time: " << curTime << '\n'
            << "Cur Day: " << curDay << '\n'
            << "Yes Day: " << dif << '\n' << std::flush;

    char a;
    std::cin >> a; ///Program crashes after here.

    return 0;
}

///Get beginning of day that t is a part of
time_t getDay(time_t t)
{
    //Get current time
    struct tm* loctim=localtime(&t);

    if(loctim==0)
        return 0;

    //Set loctim to beginning of day
    loctim->tm_sec=0;
    loctim->tm_min=0;
    loctim->tm_hour=0;

    //Create a int from the new time
    int reval=mktime(loctim);

    //Free memory
    delete loctim;

    return reval;
}

///Calculate how many days are between begin and end
int diffDay(time_t end,time_t begin)
{
    time_t eDay=getDay(end); //Get beginning of day end is a part of
    time_t bDay=getDay(begin); //Get beginning of day begin is a part of
    time_t dif=(eDay-bDay)/(24*60*60); //Get how many days (86400 seconds)

    return dif;
}

這是我從調試中得到的一些文字。

調用堆棧

#0 77BC3242 ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#1 00000000 0x6d067ad3 in ??() (??:??)
#2 00000000 0x00000018 in ??() (??:??)
#3 77BC3080 ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#4 00000000 0x00000018 in ??() (??:??)
#5 77C60FCB ntdll!TpCheckTerminateWorker() (C:\Windows\system32\ntdll.dll:??)
#6 00000000 0x007f0000 in ??() (??:??)
#7 00000000 0x50000163 in ??() (??:??)
#8 00000000 0x00000018 in ??() (??:??)
#9 77C1AC4B ntdll!RtlReAllocateHeap() (C:\Windows\system32\ntdll.dll:??)
#10 00000000    0x007f0000 in ??() (??:??)
#11 00000000    0x50000163 in ??() (??:??)
#12 00000000    0x00000018 in ??() (??:??)
#13 77BC3080    ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#14 00000000    0x00000018 in ??() (??:??)
#15 769A9D45    msvcrt!malloc() (C:\Windows\syswow64\msvcrt.dll:??)
#16 769AF5D3    strcpy_s() (C:\Windows\syswow64\msvcrt.dll:??)
#17 769B2B18    open_osfhandle() (C:\Windows\syswow64\msvcrt.dll:??)
#18 00000000    0x00000018 in ??() (??:??)
#19 769B3C7D    msvcrt!_get_fmode() (C:\Windows\syswow64\msvcrt.dll:??)
#20 769BA6A0    msvcrt!_fsopen() (C:\Windows\syswow64\msvcrt.dll:??)
#21 00000000    0xc3458a06 in ??() (??:??)
#22 00000000    0x00000000 in ??() (??:??)

這也是來自同一版本的另一個調用堆棧。

#0 77BE708C ntdll!RtlTraceDatabaseLock() (C:\Windows\system32\ntdll.dll:??)
#1 00000000 0x6ccdaf66 in ??() (??:??)
#2 00000000 0x00000000 in ??() (??:??)

它是一些特殊的構建選項嗎? 我正在使用-std = c ++ 0x,但決定嘗試沒有它的程序,它仍然崩潰。 感謝您的幫助,我一直在努力解決這個問題。

我認為問題在於:

  struct tm* loctim=localtime(&t);
  delete loctim;

localtime返回指向靜態緩沖區的指針。 你不應該釋放它。 這導致了“未定義的行為”。 即一些數據處於不一致狀態,並可能導致程序的另一個地方崩潰,這似乎與問題沒有直接關系。

找到這些問題的一個好方法是在valgrind下運行程序。 它為您提供有關出錯的非常准確的信息 -

vlap:~/src $ valgrind ./a.out
==29314== Memcheck, a memory error detector
==29314== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==29314== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==29314== Command: ./a.out
==29314== 
==29314== Invalid free() / delete / delete[] / realloc()
==29314==    at 0x4C29E6C: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==29314==    by 0x400D2A: getDay(long) (test.cpp:44)
==29314==    by 0x400BEE: main (test.cpp:11)
==29314==  Address 0x59f5560 is 0 bytes inside data symbol "_tmbuf"
==29314==
==29314== Invalid free() / delete / delete[] / realloc()
==29314==    at 0x4C29E6C: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==29314==    by 0x400D2A: getDay(long) (test.cpp:44)
==29314==    by 0x400D4D: diffDay(long, long) (test.cpp:52)
==29314==    by 0x400C13: main (test.cpp:13)
==29314==  Address 0x59f5560 is 0 bytes inside data symbol "_tmbuf"
==29314==
==29314== Invalid free() / delete / delete[] / realloc()
==29314==    at 0x4C29E6C: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==29314==    by 0x400D2A: getDay(long) (test.cpp:44)
==29314==    by 0x400D5D: diffDay(long, long) (test.cpp:53)
==29314==    by 0x400C13: main (test.cpp:13)
==29314==  Address 0x59f5560 is 0 bytes inside data symbol "_tmbuf"
==29314==
Cur Time: 1395580379
Cur Day: 1395529200
Yes Day: 1
a
==29314==
==29314== HEAP SUMMARY:
==29314==     in use at exit: 0 bytes in 0 blocks
==29314==   total heap usage: 12 allocs, 15 frees, 1,846 bytes allocated
==29314==
==29314== All heap blocks were freed -- no leaks are possible
==29314==
==29314== For counts of detected and suppressed errors, rerun with: -v
==29314== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 3 from 3)

你不能使用delete(一個c ++運算符)來釋放不使用c ++內存管理的localtime()的結果。 在任何情況下,您實際上不需要釋放localtime返回的值。

您可以使用cmd或終端在cmd:echo%time%> time.txt和linux終端上的文件中獲取時間:date> time.txt您可以使用:system(command)運行commsnd而不是您讀取文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM