Memory problem

M

Mohsen

Dear all,

I have used pointer in my program and I have a problem with shuffling
the values. The code is like:

..
..
..
for(i=0;i<S;++i)
{
k=i;
for (j=i+1;j<S;++j)
{
if (s[j].GN>s[k].GN) k=j;
}
Temp=s;
s=s[k]; <<<<<<<<line that has error (as Valgrind says)
s[k]=Temp;
}
..
..
..

When I am checking the program with "Valgrind" it gives me the
following error message:

==25618== Source and destination overlap in memcpy(0x4A49BA8,
0x4A49BA8, 688)
==25618== at 0x4906C4A: memcpy (mac_replace_strmem.c:394)
==25618== by 0x4020E4: Shuffle(Record*, Record*) (109.cpp:885)
==25618== by 0x40DF21: main (109.cpp:229)

Does anyone have any idea how I can prevent this error?

Thanks,
Mohsen
 
R

Rolf Magnus

Mohsen said:
Dear all,

I have used pointer in my program and I have a problem with shuffling
the values. The code is like:

.
.
.
for(i=0;i<S;++i)
{
k=i;
for (j=i+1;j<S;++j)
{
if (s[j].GN>s[k].GN) k=j;
}
Temp=s;
s=s[k]; <<<<<<<<line that has error (as Valgrind says)
s[k]=Temp;
}
.
.
.

When I am checking the program with "Valgrind" it gives me the
following error message:

==25618== Source and destination overlap in memcpy(0x4A49BA8,
0x4A49BA8, 688)
==25618== at 0x4906C4A: memcpy (mac_replace_strmem.c:394)
==25618== by 0x4020E4: Shuffle(Record*, Record*) (109.cpp:885)
==25618== by 0x40DF21: main (109.cpp:229)

Does anyone have any idea how I can prevent this error?


Not without any more information about the type of s. Looking your code
fragment and the message, it seems to me that that type's operator= uses
memcpy, and if k=i, the object is copied to itself, which would mean
that "source and destination overlap", which is not allowed for memcpy.
 
P

Pete Becker

Mohsen said:
Dear all,

I have used pointer in my program and I have a problem with shuffling
the values. The code is like:

.
.
.
for(i=0;i<S;++i)
{
k=i;
for (j=i+1;j<S;++j)
{
if (s[j].GN>s[k].GN) k=j;
}
Temp=s;
s=s[k]; <<<<<<<<line that has error (as Valgrind says)
s[k]=Temp;
}
.
.
.

When I am checking the program with "Valgrind" it gives me the
following error message:

==25618== Source and destination overlap in memcpy(0x4A49BA8,
0x4A49BA8, 688)
==25618== at 0x4906C4A: memcpy (mac_replace_strmem.c:394)
==25618== by 0x4020E4: Shuffle(Record*, Record*) (109.cpp:885)
==25618== by 0x40DF21: main (109.cpp:229)

Does anyone have any idea how I can prevent this error?


Sure. Remove the call to memcpy.

For any value of i, if the condition inside the inner loop is never
true, the code after the inner loop swaps a value with itself. There's
nothing inherently wrong with that -- usually its just a waste of time.
The problem here is that your compiler (I assume) has helpfully
rewritten that assignment as a call to memcpy, but memcpy does not
support overlapping ranges. So it's turned something that's valid into
something that isn't.

To avoid it, if you can't change compilers, change the code to not
exchange the values when k == i.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
K

Kai-Uwe Bux

Mohsen said:
Dear all,

I have used pointer in my program and I have a problem with shuffling
the values. The code is like:

.
.
.
for(i=0;i<S;++i)
{
k=i;
for (j=i+1;j<S;++j)
{
if (s[j].GN>s[k].GN) k=j;
}
Temp=s;
s=s[k]; <<<<<<<<line that has error (as Valgrind says)
s[k]=Temp;
}
.
.
.

When I am checking the program with "Valgrind" it gives me the
following error message:

==25618== Source and destination overlap in memcpy(0x4A49BA8,
0x4A49BA8, 688)
==25618== at 0x4906C4A: memcpy (mac_replace_strmem.c:394)
==25618== by 0x4020E4: Shuffle(Record*, Record*) (109.cpp:885)
==25618== by 0x40DF21: main (109.cpp:229)

Does anyone have any idea how I can prevent this error?


Your code allows for the assignment

s = s;

to happen. It appears that your assignment operator for the class of s is
not good for self-assignment. Fix that, and the error will go away. A
classical method is

s_class & operator= ( s_class const & rhs ) {
if ( this != &rhs ) {
// do the assignment
}
return ( *this );
}

You could also consider the copy-swap idiom.


Best

Kai-Uwe Bux
 
M

Martin Steen

Mohsen said:
Does anyone have any idea how I can prevent this error?

Thanks,
Mohsen

Try this:

if (i != k)
{
Temp=s;
s=s[k];
s[k]=Temp;
}

Best regards, Martin
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top