gpt4 book ai didi

c - 释放/解除分配缓冲区时出现段错误

转载 作者:行者123 更新时间:2023-11-30 15:49:25 31 4
gpt4 key购买 nike

当我尝试释放缓冲区时遇到问题。每次我尝试将缓冲区传递给释放方法时,都会发生段错误。 Valgrind 确认段错误位于 BufferDeallocate 方法中。

==30960== Memcheck, a memory error detector                                     
==30960== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==30960== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==30960== Command: ./a.out
==30960==
==30960== Conditional jump or move depends on uninitialised value(s)
==30960== at 0x40178A6: index (in /lib64/ld-2.15.so)
==30960== by 0x4007642: expand_dynamic_string_token (in /lib64/ld-2.15.so)
==30960== by 0x4007EFD: _dl_map_object (in /lib64/ld-2.15.so)
==30960== by 0x400135D: map_doit (in /lib64/ld-2.15.so)
==30960== by 0x400E345: _dl_catch_error (in /lib64/ld-2.15.so)
==30960== by 0x4001276: do_preload (in /lib64/ld-2.15.so)
==30960== by 0x4003C11: dl_main (in /lib64/ld-2.15.so)
==30960== by 0x40149A7: _dl_sysdep_start (in /lib64/ld-2.15.so)
==30960== by 0x40049DC: _dl_start (in /lib64/ld-2.15.so)
==30960== by 0x4000BA7: ??? (in /lib64/ld-2.15.so)
==30960==
==30960== Conditional jump or move depends on uninitialised value(s)
==30960== at 0x40178AB: index (in /lib64/ld-2.15.so)
==30960== by 0x4007642: expand_dynamic_string_token (in /lib64/ld-2.15.so)
==30960== by 0x4007EFD: _dl_map_object (in /lib64/ld-2.15.so)
==30960== by 0x400135D: map_doit (in /lib64/ld-2.15.so)
==30960== by 0x400E345: _dl_catch_error (in /lib64/ld-2.15.so)
==30960== by 0x4001276: do_preload (in /lib64/ld-2.15.so)
==30960== by 0x4003C11: dl_main (in /lib64/ld-2.15.so)
==30960== by 0x40149A7: _dl_sysdep_start (in /lib64/ld-2.15.so)
==30960== by 0x40049DC: _dl_start (in /lib64/ld-2.15.so)
==30960== by 0x4000BA7: ??? (in /lib64/ld-2.15.so)
==30960==
==30960== Thread 2:
==30960== Use of uninitialised value of size 8
==30960== at 0x400C97: bufferRead (in /home/cward/cs370/Project04/a.out)
==30960== by 0x4E39D95: start_thread (in /lib64/libpthread-2.15.so)
==30960==
==30960== Thread 1:
==30960== Invalid read of size 4
==30960== at 0x400919F: do_lookup_x (in /lib64/ld-2.15.so)
==30960== by 0x4009B31: _dl_lookup_symbol_x (in /lib64/ld-2.15.so)
==30960== by 0x400D880: _dl_fixup (in /lib64/ld-2.15.so)
==30960== by 0x4014154: _dl_runtime_resolve (in /lib64/ld-2.15.so)
==30960== by 0x400AAA: main (in /home/cward/cs370/Project04/a.out)
==30960== Address 0x6f57206f6c6c6550 is not stack'd, malloc'd or (recently) free'd
==30960==
==30960==
==30960== Process terminating with default action of signal 11 (SIGSEGV)
==30960== General Protection Fault
==30960== at 0x400919F: do_lookup_x (in /lib64/ld-2.15.so)
==30960== by 0x4009B31: _dl_lookup_symbol_x (in /lib64/ld-2.15.so)
==30960== by 0x400D880: _dl_fixup (in /lib64/ld-2.15.so)
==30960== by 0x4014154: _dl_runtime_resolve (in /lib64/ld-2.15.so)
==30960== by 0x400AAA: main (in /home/cward/cs370/Project04/a.out)
[boundedbuffer] Added: Hello World!

[boundedbuffer] in bRead
[main] Printing from out_array:
Hello World!

==30960==
==30960== HEAP SUMMARY:
==30960== in use at exit: 1,872 bytes in 2 blocks
==30960== total heap usage: 3 allocs, 1 frees, 2,144 bytes allocated
==30960==

我将提供所有文件,以便每个人都可以看到代码方面发生了什么。

这是我的 BoundedBuffer.c 文件:

<snip>

BoundedBuffer.h

<snip>

这是我们的主文件,用于测试 BoundedBuffer。

主.c:

<snip>  

任何有关解决此问题的建议都会很棒!我对 C 编码还很陌生,释放/分配是 C 中令我困惑的主题之一。

最佳答案

在 Main.c 中,您设置了 readParams.b = writeParams.b; 这会在您尝试解除分配时导致错误。 C的free方法并不像C++的delete,实际上是从 here ,因为您尝试两次释放相同的数据,所以会导致未定义的行为

换句话说,由于读取和写入参数缓冲区都指向同一个对象,因此您应该只 bufferDeallocate 其中一个,因为一旦内存被释放,您就无法再控制它并尝试释放内存不控制就是疯狂。

关于c - 释放/解除分配缓冲区时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16308403/

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