c - 在不中止核心的情况下释放 C 中的内存

标签 c

<分区>

我刚刚用 C 语言为我正在处理的项目编写了这段代码,一切正常,唯一的问题是我有内存泄漏。我是 C 的新手,我所做的自由尝试只是以核心被丢弃而告终。谁能告诉我在哪里免费并解释为什么?我以为你只是担心释放任何你的 malloc/指针/数组/结构,但我试图释放任何这些都失败了。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <utime.h>
#include <limits.h>


int main(int argc, char **argv)
{
    int i, status = 0;
    struct stat statbuf;
    struct tm *info;
    char buf[80];
    time_t now = time (NULL);

    for (i = 1; i < argc; i++) {
    if (lstat(argv[i], &statbuf)) {
        perror(argv[i]);
        status = 1;
    } else {

        printf("%6o ", statbuf.st_mode);
            int size = statbuf.st_size;
            printf("%9d",  size);    
            info = localtime(&statbuf.st_mtime);
            if(&now-15552000 < &statbuf.st_mtime){
             strftime(buf, 80, "%b %e %R", info);
             printf(" %s ", buf );}

            else{
            strftime(buf, 80, "%b %e  %Y", info);
            printf(" %s ", buf );}
            if (argc == 1) {
            printf(".\n");}
        else if (S_ISREG(statbuf.st_mode))
        printf("%s\n", argv[i]);

            char *pathname = argv[i];
            char linkname[PATH_MAX + 1];
            ssize_t retval = readlink (pathname, linkname, sizeof linkname);
            if (retval >= 0) {
            linkname[retval < PATH_MAX + 1 ? retval : PATH_MAX] = '\0';
            printf ("%s -> \"%s\"\n", pathname, linkname);

      }     
    }
    }

    return(status);
}

我运行 Valgrind 来检查内存泄漏,这是我收到的消息。抱歉,我知道它有点长:

> ==7314== Memcheck, a memory error detector
> ==7314== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
> ==7314== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
> ==7314== Command: 18stat.c
> ==7314==  18stat.c: line 14: syntax error near unexpected token `(' 18stat.c: line 14: `int main(int argc, char **argv)'
> ==7314== 
> ==7314== HEAP SUMMARY:
> ==7314==     in use at exit: 30,109 bytes in 664 blocks
> ==7314==   total heap usage: 766 allocs, 102 frees, 42,054 bytes allocated
> ==7314== 
> ==7314== 108 (32 direct, 76 indirect) bytes in 1 blocks are definitely lost in loss record 195 of 214
> ==7314==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
> ==7314==    by 0x465EA2: xmalloc (in /bin/bash)
> ==7314==    by 0x428D6A: make_bare_simple_command (in /bin/bash)
> ==7314==    by 0x42943D: make_simple_command (in /bin/bash)
> ==7314==    by 0x4261E7: yyparse (in /bin/bash)
> ==7314==    by 0x41D2E9: parse_command (in /bin/bash)
> ==7314==    by 0x41D3B5: read_command (in /bin/bash)
> ==7314==    by 0x41D607: reader_loop (in /bin/bash)
> ==7314==    by 0x41CCF8: main (in /bin/bash)
> ==7314== 
> ==7314== LEAK SUMMARY:
> ==7314==    definitely lost: 32 bytes in 1 blocks
> ==7314==    indirectly lost: 76 bytes in 5 blocks
> ==7314==      possibly lost: 0 bytes in 0 blocks
> ==7314==    still reachable: 30,001 bytes in 658 blocks
> ==7314==         suppressed: 0 bytes in 0 blocks
> ==7314== Reachable blocks (those to which a pointer was found) are not shown.
> ==7314== To see them, rerun with: --leak-check=full --show-reachable=yes
> ==7314== 
> ==7314== For counts of detected and suppressed errors, rerun with: -v
> ==7314== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)

最佳答案

确保在二进制文件而不是源代码上运行 valgrind。

关于c - 在不中止核心的情况下释放 C 中的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20254946/

相关文章:

c - sbrk(0) 和 sbrk(size) 都返回相同的地址

c - ncurses 菜单 - 不会显示我的用户输入的字符串

我可以根据 C 标准分配超过 65535 个字节吗?

C - Linux - Pthreads 和信号量

c++ - directx 9 与 Visual Studio 2012 Express

c - OpenMPI 矩阵乘法

c - 具有可变目标的 makefile

c++ - 如何使用 LLVM API 获取全局变量的初始化器

c++ - 如何正确删除一个空指针对象?

CS50贪心算法