473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Memory problem

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[i];
s[i]=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

Oct 9 '06 #1
4 1674
Mohsen wrote:
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[i];
s[i]=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.

Oct 9 '06 #2
Mohsen wrote:
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[i];
s[i]=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.
Oct 9 '06 #3
Mohsen wrote:
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[i];
s[i]=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[i] = s[i];

to happen. It appears that your assignment operator for the class of s[i] 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

Oct 9 '06 #4
Mohsen wrote:
Does anyone have any idea how I can prevent this error?

Thanks,
Mohsen
Try this:

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

Best regards, Martin

Oct 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Amadeus | last post by:
Hello Everybody! I have a problem with MySQL servers running RedHat 9 (smp kernel 2.4.20) on Intel and MySQL server 4.0.14 (problem also appears on binary distr 4.0.15 and on 4.0.15 I bilt myself...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
16
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. ...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
9
by: Bruno Barberi Gnecco | last post by:
I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM XML), and I ended up using PHP to integrate with the rest of the code that already runs the...
9
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It...
17
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.