gpt4 book ai didi

c - 个人 Valgrind 异常

转载 作者:太空宇宙 更新时间:2023-11-04 04:05:22 31 4
gpt4 key购买 nike

首先,我想提前感谢您在此问题上提供的任何帮助。

Valgrind下面粘贴的输出源自以下单行 C 代码。

for( j=i;j<list->size-1;j++ ) s3->delete_tail( s3 );

但是,如果我将行更改为:

for( j=i;j>=0;j-- ) s3->delete_tail( s3 );

这只是for循环参数的变化,Valgrind中的错误未报告以下输出。我不想天真地认为这与 for 循环有关。我已经在程序中的不同点测试了 delete tail,删除了不同数量的数据,没有报告任何错误。所以我的直觉是问题出在我程序的其他地方。我一直在寻找几个小时,但似乎无法找到它。我是一名新程序员,所以这可以明确归因于我缺乏经验。

为了提供更多上下文,这里是周围的代码。

MatrixList* gen_parens( MatrixList *list, MatrixList *ret ) {
if( list->size==1 ) {
}
if( list->size==2 ) {
}
int i=0;
//for( i=0;i<list->size;i++ ) {
MatrixList *s3 = (MatrixList*)malloc(sizeof(MatrixList));
MatrixList *s2 = (MatrixList*)malloc(sizeof(MatrixList));
set_list_functions( s3 );
set_list_functions( s2 );

list->clone( list, s3 );
list->clone( list, s2 );

int j=0;
//for( j=i;j<list->size-1;j++ ) s3->delete_tail( s3 );
for( j=i;j>=0;j-- ) s3->delete_tail( s3 );
for( j=i;j>=0;j-- ) s2->delete_head( s2 );

s3->print( s3 );
s2->print( s2 );

s3->release( s3 );
free( s3 );
s2->release( s2 );
free( s2 );

ret=(MatrixList*)malloc(sizeof(MatrixList));
set_list_functions( ret );
//}
return ( ret );
}

如果您需要帮助,源链接位于 http://matthewh.me/Scripts/c++/matrix_chain/。密码= guest ,用户= guest 。

Valgrind从这篇文章的第一行代码中检测到错误的输出是:

[mehoggan@desktop matrix_chain]$ valgrind --leak-check=full -v ./main
==3317== Memcheck, a memory error detector
==3317== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3317== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==3317== Command: ./main
==3317==
--3317-- Valgrind options:
--3317-- --leak-check=full
--3317-- -v
--3317-- Contents of /proc/version:
--3317-- Linux version 2.6.35.13-92.fc14.i686.PAE (mockbuild@x86-17.phx2.fedoraproject.org) (gcc version 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC) ) #1 SMP Sat May 21 17:33:09 UTC 2011
--3317-- Arch and hwcaps: X86, x86-sse1-sse2
--3317-- Page sizes: currently 4096, max supported 4096
--3317-- Valgrind library directory: /usr/lib/valgrind
--3317-- Reading syms from /lib/ld-2.13.so (0x799000)
--3317-- Reading debug info from /usr/lib/debug/lib/ld-2.13.so.debug ..
--3317-- Reading syms from /home/mehoggan/Subversion/Scripts/c++/matrix_chain/main (0x8048000)
--3317-- Reading syms from /usr/lib/valgrind/memcheck-x86-linux (0x38000000)
--3317-- object doesn't have a dynamic symbol table
--3317-- Reading suppressions file: /usr/lib/valgrind/default.supp
--3317-- REDIR: 0x7b0080 (index) redirected to 0x3803dd33 (vgPlain_x86_linux_REDIR_FOR_index)
--3317-- Reading syms from /usr/lib/valgrind/vgpreload_core-x86-linux.so (0x4001000)
--3317-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so (0x4003000)
==3317== WARNING: new redirection conflicts with existing -- ignoring it
--3317-- new: 0x007b0080 (index ) R-> 0x04006bb0 index
--3317-- REDIR: 0x7b0240 (strlen) redirected to 0x4006fe0 (strlen)
--3317-- Reading syms from /lib/libc-2.13.so (0x7ba000)
--3317-- Reading debug info from /usr/lib/debug/lib/libc-2.13.so.debug ..
--3317-- REDIR: 0x8306d0 (rindex) redirected to 0x40069f0 (rindex)
--3317-- REDIR: 0x82c010 (malloc) redirected to 0x40066f9 (malloc)
--3317-- REDIR: 0x8302b0 (strlen) redirected to 0x400146d (_vgnU_ifunc_wrapper)
--3317-- REDIR: 0x837510 (__strlen_sse2_bsf) redirected to 0x4006fa0 (strlen)
--3317-- REDIR: 0x82fd70 (strcpy) redirected to 0x4007020 (strcpy)
==3317== Conditional jump or move depends on uninitialised value(s)
==3317== at 0x8048EFB: gen_parens (matrix_list.c:187)
==3317== by 0x80486EC: main (main.c:36)
==3317==
Deleteing Tail
--3317-- REDIR: 0x82c530 (free) redirected to 0x4005a85 (free)
Deleteing Tail
Deleteing Tail
Deleteing Tail
Deleteing Tail
--3317-- REDIR: 0x833000 (strchrnul) redirected to 0x4008a20 (strchrnul)
A(1X3)
B(3X5), C(5X7), D(7X9), E(9X2), F(2X6)
Nothing to clean up!!!
==3317==
==3317== HEAP SUMMARY:
==3317== in use at exit: 0 bytes in 0 blocks
==3317== total heap usage: 58 allocs, 58 frees, 824 bytes allocated
==3317==
==3317== All heap blocks were freed -- no leaks are possible
==3317==
==3317== Use --track-origins=yes to see where uninitialised values come from
==3317== ERROR SUMMARY: 6 errors from 1 contexts (suppressed: 12 from 8)
==3317==
==3317== 6 errors in context 1 of 1:
==3317== Conditional jump or move depends on uninitialised value(s)
==3317== at 0x8048EFB: gen_parens (matrix_list.c:187)
==3317== by 0x80486EC: main (main.c:36)
==3317==
--3317--
--3317-- used_suppression: 12 dl-hack3-cond-1
==3317==
==3317== ERROR SUMMARY: 6 errors from 1 contexts (suppressed: 12 from 8)

更新

我想我找到了部分问题,将继续测试。但是我没有在 main.c 中初始化列表的大小。我在函数中添加了以下几行

void set_list_functions( MatrixList *list );

matrix_list.c 内部

void set_list_functions( MatrixList *list ) {
list->head = NULL;
list->tail = NULL;
list->append = append;
list->print = print;
list->reverse_print = reverse_print;
list->delete = delete;
list->delete_head = delete_head;
list->delete_tail = delete_tail;
list->release = release;
list->clone = clone;
***list->size = 0;***
}

最佳答案

Conditional jump or move depends on uninitialised value(s)

这意味着您正在使用一个尚未初始化的值。现在让我们看看您的两个循环:

无错误:

for( j=i;j>=0;j-- ) s3->delete_tail( s3 );

错误:

for( j=i;j<list->size-1;j++ ) s3->delete_tail( s3 );

如果错误因语法更改而消失,则错误必须出现在此处。唯一的区别是一个包含 list->size,而另一个不包含。

这在错误的上下文中意味着什么? list->size 没有正确赋值或初始化,这就是你得到条件跳转错误的原因。因此,请查看您的初始化函数,看看有什么问题。

此外,这里还有一些额外的建议。你有:

i=0
....
j=0

在你的循环中你有 j=i,这只会让事情变得困惑。只需执行您的功能即可:

j=0;
for (j; j >=0; j--)

这让代码更清晰。将 i=0 移到需要的地方,如果不使用则将其删除。

关于c - 个人 Valgrind 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6636061/

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