开发者

Personal Valgrind Anomaly

First I would like to thank in you in advance for any help in this matter.

The Valgrind output pasted below is stemming from the following single line of C code.

for( j=i;j<list->size-1;j++ ) s3->del开发者_如何学Goete_tail( s3 );

However, if I change the line to lets say,

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

which is just a change in the parameters of the for loop, the errors in the Valgrind output below are not reported. I do not want to be naive about this and think that it has something to do with the for loop. I have tested the delete tail at various points in the program deleting various amounts of data with no errors being reported. So my hunch is that the problem lies somewhere else in my program. I have been looking for hours but can't seem to find it. I am a new programmer so this can definitively attributed to my lack of experience.

To provide a little more context, here is the surrounding code.

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 );
}

A link to the source if you need it to help is located at http://matthewh.me/Scripts/c++/matrix_chain/ with password=guest,user=guest.

The Valgrind output with the detected errors from the first line of code in this post is:

[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)

UPDATE

I think I found part of the problem, going to keep testing. But I was not initializing the size of the list in main.c. I added the following lines to the function

void set_list_functions( MatrixList *list );

inside of 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)

This means that you are using a value that has not been initialized. Now let's look at your two loops:

Error free:

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

Error:

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

If the error goes away due to a change in syntax, then the error must be present here. The only differences are that one contains list->size, while the other doesn't.

What does this mean in the context of the error? list->size hasn't been properly assigned a value or initialized, which is why you get the conditional jump error. So look at your initializing functions and see what's the issue.

Also, here is some extra advice. You have:

i=0
....
j=0

And in your loops you have j=i, which is just making things confusion. Just make your function:

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

which makes the code clearer. Move i=0 where it's needed, or delete it if it isn't used.


I read through a portion of your repository code and tried to keep track of what you are doing. There is a major problem here:

void clone_list( MatrixList *from, MatrixList *to ) {
  if( from->head == NULL ) {
    to = NULL;
  } else {
    *to = *from;
    to->head = 0;
    to->tail = 0;
    to->size = 0;
    Node *old; 
    for( old=from->head;old!= NULL;old=old->next ) {
      Matrix *m_copy = clone_matrix(old->M);
      to->append( to,m_copy );
    }
  }
}

The first if part with to = NULL will change nothing of your return value, because you only assign it locally. If you need to change the pointer, then make the parameter a MatrixList **to and then do a *to = NULL. But I think it may also make sense to only write do a to->size = 0 (and check to for beeing NULL before).

If from was NULL, then list->size will be undefined later, because you dont initialize it before, what means it could have every possible value.


Before you do this:

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

you should check for list->size = 0. That's likely the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜