0

現在の時刻を保持するために1つの変数を割り当てています。

struct tm *cur = malloc (sizeof (cur));
time_t t = time (NULL);
localtime_r (&t, cur);

次に、年を印刷します。合ってます。次に、ファイルから新しい可変時間値を割り当てるループに入ります。

struct stat file_stats;
struct tm *file = malloc (sizeof (file));
lstat (argv[itor], &file_stats);
//check1
localtime_r(&file_stats.st_mtime, file);
//check2

「check1」でcur->tm_year、正確で妥当な値を出力します。「check2」でcur->tm_year「0」を出力します。ここで何が起きてるの?ポインタ操作で何かが足りないのと関係があると思います。どんな助けでも、特に私が誤解していることの説明をいただければ幸いです。

4

1 に答える 1

2

私はあなたのコードをこのSSCCEに適合させました:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>

int main(int argc, char **argv)
{
    struct tm *cur = malloc(sizeof(cur));
    time_t t = time (NULL);
    localtime_r(&t, cur);

    printf("curr 1 year: %d\n", cur->tm_year + 1900);

    for (int itor = 1; itor < argc; itor++)
    {
        struct stat file_stats;
        struct tm *file = malloc(sizeof(file));
        file->tm_year = 0;
        if (lstat(argv[itor], &file_stats) == 0)
        {
            printf("curr 2 year: %d\n", cur->tm_year + 1900);
            localtime_r(&file_stats.st_mtime, file);
            printf("curr 3 year: %d\n", cur->tm_year + 1900);
            printf("file 1 year: %d\n", file->tm_year + 1900);
        }
    }
    return(0);
}

私が実行した結果は次のとおりです。

curr 1 year: 2013
curr 2 year: 2013
curr 3 year: 2013
file 1 year: 2013
curr 2 year: 2013
curr 3 year: 2013
file 1 year: 2010
curr 2 year: 2013
curr 3 year: 2013
file 1 year: 2013
curr 2 year: 2013
curr 3 year: 2013
file 1 year: 2011

古いファイルがいくつかあったので便利でした。表面的には問題ないようです。しかし、valgrind適合しました:

==50495== Memcheck, a memory error detector
==50495== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==50495== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==50495== Command: ltr 3d.c const-stuff.c ltr.c madump.c pthread-1.c pthread-2.c pthread-3.c recv.c regress.c send.c strandsort.c x.c
==50495== 
==50495== WARNING: Support on MacOS 10.8 is experimental and mostly broken.
==50495== WARNING: Expect incorrect results, assertions and crashes.
==50495== WARNING: In particular, Memcheck on 32-bit programs will fail to
==50495== WARNING: detect any errors associated with heap-allocated data.
==50495== 
==50495== Invalid write of size 4
==50495==    at 0x105C48: timesub (in /usr/lib/system/libsystem_c.dylib)
==50495==    by 0x1058FE: _st_localsub (in /usr/lib/system/libsystem_c.dylib)
==50495==    by 0x10609D: localtime_r (in /usr/lib/system/libsystem_c.dylib)
==50495==    by 0x100000DE8: main (ltr.c:10)
==50495==  Address 0x10001b188 is 0 bytes after a block of size 8 alloc'd
==50495==    at 0x5686: malloc (vg_replace_malloc.c:274)
==50495==    by 0x100000DC3: main (ltr.c:8)
==50495== 
==50495== Invalid write of size 4
==50495==    at 0x105C70: timesub (in /usr/lib/system/libsystem_c.dylib)
==50495==    by 0x1058FE: _st_localsub (in /usr/lib/system/libsystem_c.dylib)
==50495==    by 0x10609D: localtime_r (in /usr/lib/system/libsystem_c.dylib)
==50495==    by 0x100000DE8: main (ltr.c:10)
==50495==  Address 0x10001b198 is 16 bytes after a block of size 8 alloc'd
==50495==    at 0x5686: malloc (vg_replace_malloc.c:274)
==50495==    by 0x100000DC3: main (ltr.c:8)
==50495== 

そして、出力はかなり長い間同様の流れで続きました。しかし、それは問題を浮き彫りにします:あなたは電話でsizeof(*cur)そしてを使うべきです。その結果、次のようになります。sizeof(*file)malloc()

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>

int main(int argc, char **argv)
{
    struct tm *cur = malloc(sizeof(*cur));
    time_t t = time (NULL);
    localtime_r(&t, cur);

    printf("curr 1 year: %d\n", cur->tm_year + 1900);

    for (int itor = 1; itor < argc; itor++)
    {
        struct stat file_stats;
        struct tm *file = malloc(sizeof(*file));
        file->tm_year = 0;
        if (lstat(argv[itor], &file_stats) == 0)
        {
            printf("curr 2 year: %d\n", cur->tm_year + 1900);
            localtime_r(&file_stats.st_mtime, file);
            printf("curr 3 year: %d\n", cur->tm_year + 1900);
            printf("file 1 year: %d\n", file->tm_year + 1900);
        }
    }
    return(0);
}

そして、それにvalgrindきれいな健康法案を与えます。

于 2013-02-26T06:08:39.673 に答える