Valgrind Error Report

M

Mohsen

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!
 
D

Dave Townsend

Mohsen said:
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;
 
M

Mohsen

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 said:
Mohsen said:
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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top