Сначала я хотел бы заранее поблагодарить вас за любую помощь в этом вопросе.
Вывод 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.Я проверил хвост удаления в разных точках программы, удаляя различные объемы данных без ошибок.Так что я догадываюсь, что проблема лежит где-то еще в моей программе.Я искал часы, но не могу найти это.Я новый программист, так что это может быть определенно связано с моим отсутствием опыта.
Чтобы обеспечить немного больше контекста, вот окружающий код.
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/ с паролем = гость, пользователь = гость.
Вывод 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;***
}