Valgrind Error Report

Discussion in 'C++' started by Mohsen, Sep 26, 2006.

  1. Mohsen

    Mohsen Guest

    Dear all,
    I am running a program and in one of the functions I have to sort the
    data of an array as follows:
    ..
    ..
    ..
    for (m=0;m<M;++m) //Sort
    {
    k=m;
    for (j=m+1;j<M;++j)
    {
    if (DT[j]<DT[k]) k=j;
    }
    Temp=DT[m];
    DT[m]=DT[k]; <<-----The 'Valgrind' gives error in this line
    DT[k]=Temp;
    }
    ..
    ..
    ..

    When I check my program by 'Valgrind', it gives me the following
    sentence which is pointing to error in the line that I have pointed in
    the program.
    ==11285==
    ==11285== Source and destination overlap in memcpy(0x4A33358,
    0x4A33358, 400)
    ==11285== at 0x4906C4A: memcpy (mac_replace_strmem.c:394)
    ==11285== by 0x4014F8: SP(Record*, Record*) (925.cpp:601)
    ==11285== by 0x40EDF5: main (925.cpp:197)

    Is it really an error? I have just substituted the elements of an array!
     
    Mohsen, Sep 26, 2006
    #1
    1. Advertising

  2. "Mohsen" <> wrote in message
    news:...
    > Dear all,
    > I am running a program and in one of the functions I have to sort the
    > data of an array as follows:
    > .
    > .
    > .
    > for (m=0;m<M;++m) //Sort
    > {
    > k=m;
    > for (j=m+1;j<M;++j)
    > {
    > if (DT[j]<DT[k]) k=j;
    > }
    > Temp=DT[m];
    > DT[m]=DT[k]; <<-----The 'Valgrind' gives error in this line
    > DT[k]=Temp;
    > }
    > .
    > .
    > .
    >
    > When I check my program by 'Valgrind', it gives me the following
    > sentence which is pointing to error in the line that I have pointed in
    > the program.
    > ==11285==
    > ==11285== Source and destination overlap in memcpy(0x4A33358,
    > 0x4A33358, 400)
    > ==11285== at 0x4906C4A: memcpy (mac_replace_strmem.c:394)
    > ==11285== by 0x4014F8: SP(Record*, Record*) (925.cpp:601)
    > ==11285== by 0x40EDF5: main (925.cpp:197)
    >
    > Is it really an error? I have just substituted the elements of an array!
    >

    In this case its harmless. I believe valgrind issues warnings about
    overlapping
    src/dst when using memcpy (which has undefined behavior in that situation),
    you should use memmove when src/dst overlap.
    In this case src and dst are the same. But why is that,shouldn't you only
    be swapping elements which are actually different ?
    Shouldn't the line be if (DT[j]<=DT[k]) k=j;
     
    Dave Townsend, Sep 26, 2006
    #2
    1. Advertising

  3. Mohsen

    Mohsen Guest

    Hi,
    The values af my array can not be the same and for that reason I have
    written:
    if (DT[j]<DT[k]) k=j;
    Best,
    Mohsen

    Dave Townsend wrote:
    > "Mohsen" <> wrote in message
    > news:...
    > > Dear all,
    > > I am running a program and in one of the functions I have to sort the
    > > data of an array as follows:
    > > .
    > > .
    > > .
    > > for (m=0;m<M;++m) //Sort
    > > {
    > > k=m;
    > > for (j=m+1;j<M;++j)
    > > {
    > > if (DT[j]<DT[k]) k=j;
    > > }
    > > Temp=DT[m];
    > > DT[m]=DT[k]; <<-----The 'Valgrind' gives error in this line
    > > DT[k]=Temp;
    > > }
    > > .
    > > .
    > > .
    > >
    > > When I check my program by 'Valgrind', it gives me the following
    > > sentence which is pointing to error in the line that I have pointed in
    > > the program.
    > > ==11285==
    > > ==11285== Source and destination overlap in memcpy(0x4A33358,
    > > 0x4A33358, 400)
    > > ==11285== at 0x4906C4A: memcpy (mac_replace_strmem.c:394)
    > > ==11285== by 0x4014F8: SP(Record*, Record*) (925.cpp:601)
    > > ==11285== by 0x40EDF5: main (925.cpp:197)
    > >
    > > Is it really an error? I have just substituted the elements of an array!
    > >

    > In this case its harmless. I believe valgrind issues warnings about
    > overlapping
    > src/dst when using memcpy (which has undefined behavior in that situation),
    > you should use memmove when src/dst overlap.
    > In this case src and dst are the same. But why is that,shouldn't you only
    > be swapping elements which are actually different ?
    > Shouldn't the line be if (DT[j]<=DT[k]) k=j;
     
    Mohsen, Sep 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre

    leak detected using valgrind

    Alexandre, Nov 13, 2003, in forum: C++
    Replies:
    1
    Views:
    323
    Gianni Mariani
    Nov 13, 2003
  2. rsina
    Replies:
    2
    Views:
    1,064
    ettipmoez
    Nov 29, 2004
  3. Andreas Andersen

    Problems with valgrind

    Andreas Andersen, Jan 18, 2005, in forum: C++
    Replies:
    2
    Views:
    330
    Gernot Frisch
    Jan 18, 2005
  4. Replies:
    2
    Views:
    34,073
    Victor Bazarov
    Feb 17, 2005
  5. Replies:
    1
    Views:
    466
    Christoph Bartoschek
    Apr 29, 2005
Loading...

Share This Page