Pointers c99指针和本地时间问题\r

Pointers c99指针和本地时间问题\r,pointers,c99,localtime,time.h,Pointers,C99,Localtime,Time.h,我指定一个变量来保存当前时间: 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], &fi

我指定一个变量来保存当前时间:

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

在“检查1”中,
cur->tm_year
打印正确合理的值。在“检查2”处,
cur->tm_year
打印“0”。这里发生了什么?我想这与我缺少指针操作有关。任何帮助都将不胜感激,尤其是对我误解的解释

我将您的代码改编为:

这很有用,因为我周围有一些旧文件。表面上看,似乎没有问题。然而,
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== 
在相当长的一段时间内,产量一直保持着类似的趋势。但它突出了问题:您应该在
malloc()
调用中使用
sizeof(*cur)
sizeof(*file)
。这将产生:

#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);
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
struct tm*cur=malloc(sizeof(*cur));
时间t=时间(空);
本地时间(r&t,cur);
printf(“当前1年:%d\n”,当前->tm\u年+1900);
for(int-itor=1;itortm_year=0;
if(lstat(argv[itor],&file_stats)==0)
{
printf(“当前2年:%d\n”,当前->tm\u年+1900);
localtime\r(&file\u stats.st\u mtime,file);
printf(“当前3年:%d\n”,当前->tm\u年+1900);
printf(“文件1年:%d\n”,文件->tm\u年+1900);
}
}
返回(0);
}

而且,
valgrind
提供了一份干净的健康清单。

虽然这无关紧要,但为什么要动态分配呢?为什么不分配一个
struct tm
并将其地址传递给
localtime\r()
?我不确定为什么选择使用动态分配,但我认为这确实很重要,因为在将它们更改为
struct tm
之后,事情似乎正在运行。谢谢,我猜,但是你知道为什么会这样吗?是的:你应该在
malloc()
调用中使用
sizeof(*cur)
sizeof(*file)
。我正在写一个答案,意识到这就是问题所在(多亏了
valgrind
)。哦,这很有道理。所以
malloc(sizeof(*cur))
将分配cur指向的结构所需的空间量?与以前一样,使用malloc(sizeof(cur))I只是分配指针所需的空间量。我的理解正确吗?
==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== 
#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);
}