gpt4 book ai didi

c - linux环境下c程序中关于 "Address 0x0 is not stack' d, malloc'd or (recently) free'd”的错误

转载 作者:太空狗 更新时间:2023-10-29 11:24:57 25 4
gpt4 key购买 nike

[编辑1:]

对于seqName,我在文件开头将其声明为全局参数

字符序列名[20];

[编辑2:]

那些不是在实际执行过程中传递给程序的数字吗?我只是通过使用 valgrind 工具得到了以下消息。我输入的命令是:

jl@ubuntu:~/work/dsr_analysis$ valgrind --tool=memcheck --leak-check=yes ./test

[编辑3:]

抱歉,由于我是Valgrind的新用户,所以在使用它时,我只是在Edit2中输入命令。

但是,我的程序确实有一些命令行参数。

因此,我认为我最好通过新命令来调试我的程序:

valgrind --tool=memcheck --leak-check=yes ./test foreman.cif 352 288


有一段我的程序:

height = atoi(argv[3]);

width = atoi(argv[2]);

sprintf(seqName,"%s", argv[1]);

// strcpy(seqName, argv[1]);

编译后生成一个exe文件test,然后我用Valgrind查看。然后我收到以下消息,但我不明白它想告诉我什么。任何人都可以提供一些帮助,谢谢。

jl@ubuntu:~/work/dsr_analysis$ valgrind --tool=memcheck --leak-check=yes ./test

==28940== Memcheck, a memory error detector

==28940== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.

==28940== Using Valgrind-3.6.0.SVN-Debian and LibVEX;

rerun with -h for copyright info

==28940== Command: ./test

==28940==

==28940== Invalid read of size 1

==28940== at 0x40260CA: strcpy (mc_replace_strmem.c:311)

==28940== by 0x804A5C6: main (me_search.c:1428)

==28940== Address 0x0 is not stack'd, malloc'd or (recently) free'd

==28940==

==28940==

==28940== Process terminating with default action of signal 11 (SIGSEGV)

==28940== Access not within mapped region at address 0x0

==28940== at 0x40260CA: strcpy (mc_replace_strmem.c:311)

==28940== by 0x804A5C6: main (me_search.c:1428)

==28940== If you believe this happened as a result of a stack

==28940== overflow in your program's main thread (unlikely but

==28940== possible), you can try to increase the size of the

==28940== main thread stack using the --main-stacksize= flag.

==28940== The main thread stack size used in this run was 8388608.

==28940==

==28940== HEAP SUMMARY:

==28940== in use at exit: 0 bytes in 0 blocks

==28940== total heap usage: 0 allocs, 0 frees, 0 bytes allocated

==28940==

==28940== All heap blocks were freed -- no leaks are possible

==28940==

==28940== For counts of detected and suppressed errors, rerun with: -v

==28940== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 13 from 8)1 contexts (suppressed: 13 from 8)

1 个上下文(抑制:8 个中的 13 个)

最佳答案

您确实没有发布足够清晰的代码来确定,但是如果您调用不带命令行参数的可执行文件,则 argv[1] 将包含一个 NULL 指针,argv[2]argv[3] 等将包含未定义的值。

编辑:您需要在 valgrind 命令行上为程序提供参数(我猜 - 我自己不使用 valgrind)。像这样的东西:

valgrind ... ./test foo bar zod

顺便说一句,在 Linux/Unix 上调用可执行文件 test 是个坏主意,因为它很容易与同名的内置 shell 混淆

关于c - linux环境下c程序中关于 "Address 0x0 is not stack' d, malloc'd or (recently) free'd”的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2987834/

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