gpt4 book ai didi

c - 为什么我在 malloc 一个 char* 后得到一个仍然可以访问的 block ?

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:04 25 4
gpt4 key购买 nike

我有以下代码:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

void print_usage()
{
printf("%s\n", "usage");
}

int file_exist (char *filename)
{
struct stat buffer;
return (stat (filename, &buffer) == 0);
}

int parse_parameters(int argc, char *argv[], char** in)
{
unsigned int i1 = 1; // 0 is the filename
for (; i1 < argc; ++i1)
{
if( 0 == strcmp("-h", argv[i1]) )
{
print_usage();
return 0;
}
else if( 0 == strcmp("-i", argv[i1]) )
{
*in = malloc( sizeof(char) * strlen(argv[++i1]) + 1 );
strcpy(*in, argv[i1]);
continue;
}
else
{
print_usage();
return 1;
}
}
return 0;
}

int main(int argc, char *argv[])
{
if( argc != 3 )
{
print_usage();
return 0;
}

char* in = NULL;

int parse = parse_parameters(argc, argv, &in);
if ( 0 != parse )
return parse;

printf("in: %s\n", in);

FILE* finput = NULL ;
if (file_exist(in))
finput = fopen(in, "r");
if (finput == NULL) {
perror("fopen");
exit(1);
}

free(in);
fclose(finput);
return 0;
}

在使用 valgrind 运行后,参数如下:

./main -i input

我得到以下信息:

==30977== Memcheck, a memory error detector
==30977== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==30977== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==30977== Command: ./main -i input
==30977==
in: input
fopen: No such file or directory
==30977==
==30977== HEAP SUMMARY:
==30977== in use at exit: 6 bytes in 1 blocks
==30977== total heap usage: 2 allocs, 1 frees, 574 bytes allocated
==30977==
==30977== 6 bytes in 1 blocks are still reachable in loss record 1 of 1
==30977== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30977== by 0x400946: parse_parameters (main.c:31)
==30977== by 0x4009E7: main (main.c:54)
==30977==
==30977== LEAK SUMMARY:
==30977== definitely lost: 0 bytes in 0 blocks
==30977== indirectly lost: 0 bytes in 0 blocks
==30977== possibly lost: 0 bytes in 0 blocks
==30977== still reachable: 6 bytes in 1 blocks
==30977== suppressed: 0 bytes in 0 blocks
==30977==
==30977== For counts of detected and suppressed errors, rerun with: -v
==30977== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

这是为什么呢?如果我尝试将 in 作为 char* 传递,那么在 parse_parameters 函数之后它不会被更改。

最佳答案

您的程序因 exit (1) 调用而退出,该调用发生在您 free (in) 之前。因此,您会看到 valgrind 消息。

关于c - 为什么我在 malloc 一个 char* 后得到一个仍然可以访问的 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30818506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com