473,499 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't spot source of corruption!

I have some code which when compiled on my system prints:
<output>

AnyClass Constructor for: blah

AnyClass Copy Constructor for: Copy of blah

AnyClass Copy Constructor for: Copy of Copy of blah

AnyClass Constructor for: monkey

AnyClass Assignment Operator: monkey = Copy of blah

AnyClass Assignment Operator: blah = Copy of Copy of blah

AnyClass Assignment Operator: Copy of blah = ¦.C

AnyClass Copy Constructor for: Copy of ¦.C

AnyClass Constructor for: cheese
blah
Copy of blah
monkey
Copy of ¦.C
cheese

AnyClass Destructor for: cheese

AnyClass Destructor for: Copy of ¦.C

AnyClass Destructor for: monkey

AnyClass Destructor for: Copy of Copy of blah

AnyClass Destructor for: Copy of blah

AnyClass Destructor for: blah

</output>
Note the line where it says "Copy of ¦.C". . . there's something corrupted
there. I've looked through my code a few times but can't spot who what when
where or why. Here's the code. I've highlighted with asterisks the line
which results in the corruption.

Here goes:

#include <iostream>
#include <cstddef>
#include <cstring>

class AnyClass
{
public:

static unsigned const &object_counter;
//The above is a const reference to simulate a read-only variable
unsigned to_play_with;
const char* GetName() const
{
return name;
}
private:

static unsigned object_counter_prv;
//The above is the actual non-const variable

char* name;

public:

AnyClass(const char* const in_name, unsigned const in_to_play_with = 0)
: to_play_with(in_to_play_with)
{
std::size_t length = std::strlen( in_name );

name = new char[length += 1];

memcpy(name, in_name, length);

++object_counter_prv;
std::cout << "\n AnyClass Constructor for: " << name << '\n';
}

AnyClass(AnyClass const &original) : to_play_with
(original.to_play_with)
{
std::size_t length = std::strlen( original.name );

name = new char[length += 9]; //9 = 8 + 1 (1 for the null
character)

memcpy( name, "Copy of " , 8 ); // . . .waste of a null
character

memcpy( &name[8], original.name, length -= 9 ); //Take the 9
back off

name[length += 8] = '\0';

++object_counter_prv;
std::cout << "\nAnyClass Copy Constructor for: " << name << '\n';
}

AnyClass& operator=(AnyClass const &other)
{
//NB: There is no name change whatsoever

to_play_with = other.to_play_with;

std::cout << "\n AnyClass Assignment Operator: " << name <<
" = " << other.name << '\n';
}

~AnyClass()
{
std::cout << "\n AnyClass Destructor for: " << name << '\n';

delete [] name;
--object_counter_prv;
}
};

unsigned AnyClass::object_counter_prv = 0;
unsigned const &AnyClass::object_counter = AnyClass::object_counter_prv;
int main()
{
AnyClass blah("blah");
AnyClass poo = blah;

AnyClass toke = poo;
AnyClass monkey("monkey");

monkey = poo;

AnyClass cow(poo = blah = toke); // **** CORRUPTION ***

AnyClass cheese = AnyClass("cheese");
std::cout << blah.GetName() << '\n';
std::cout << poo.GetName() << '\n';
std::cout << monkey.GetName() << '\n';
std::cout << cow.GetName() << '\n'; // *** shows corruption again ***
std::cout << cheese.GetName() << '\n';
}

Can anyone spot the error?
-JKop
Jul 22 '05 #1
3 1289
AnyClass& operator=(AnyClass const &other)
{
//NB: There is no name change whatsoever

to_play_with = other.to_play_with;

std::cout << "\n AnyClass Assignment Operator: " <<
name <<
" = " << other.name << '\n';
}


What do you notice with the above?
return *this;
-JKop
Jul 22 '05 #2
JKop <NU**@NULL.NULL> wrote in message news:<xH*******************@news.indigo.ie>...
[snip, and doing a bit of reformattng]
AnyClass& operator=(AnyClass const &other)
{
//NB: There is no name change whatsoever
to_play_with = other.to_play_with;
std::cout << "\n AnyClass Assignment Operator: " << name <<
" = " << other.name << '\n';
}


I'm going blind. I don't see a return here. Shouldn't there be one?
Socks
Jul 22 '05 #3
JKop <NU**@NULL.NULL> wrote
I have some code which when compiled on my system prints: [garbage]
AnyClass& operator=(AnyClass const &other)
{
to_play_with = other.to_play_with;
std::cout << "\n AnyClass Assignment Operator: " << name <<
" = " << other.name << '\n';
}

int main()
{
AnyClass blah("blah");
AnyClass poo = blah;
AnyClass toke = poo;
AnyClass cow(poo = blah = toke); // **** CORRUPTION ***
}


This is: AnyClass cow( poo.operator= ( blah.operator= (toke) ) );
Now, what does the operator= function return?
(You should be able to enable a warning in your compiler to
detect this situation).
Jul 22 '05 #4

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

Similar topics

0
1183
by: Frank Halley | last post by:
Greetings I am currently doing an hourly back up the backend of a split Access/Jet system onto a Linux box (using a cron script to copy and zip it up). Yes, I know this is slightly risky with...
26
2640
by: jamesbeswick | last post by:
I've been using Access since version 97 and I've migrated to 2003. I've noticed a substantial number of strange ActiveX/OLE and code corruption problems when writing databases. The only solution...
2
1327
by: Tom van Stiphout | last post by:
Hi all, I have a fairly large Access2000 application (FE/BE). One of the subforms (in datasheet) has a dropdown list named BackSplash. It's a ValueList, not LimitToList, bound to the BackSplash...
8
3390
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
11
1456
by: Tamas Demjen | last post by:
I was shocked to learn that VC++ 2005 Beta 2 can't catch Access Violation exceptions in unmanaged code. To reproduce this, I created a minimal Win32 console application: #include "stdafx.h" ...
42
2068
by: Doug | last post by:
I am in a friendly debate with some co-workers... and my boss. We use Access 2003 for the frontend (on workstations) as well as for the backend (on a Dell PowerEdge running Windows 2000 server,...
2
1810
by: Vincent | last post by:
The company I work for has developed a Microsoft Access application. Currently, it is distributed with the Microsoft Access 2002 runtime. Some of our customers have begun purchasing new Vista...
15
1537
by: Desmond | last post by:
I used Source safe in Visual Studio 6 (2001) However in .net 2008 after installing it there seems to be no sign of any source safe. Has this been dis-continued. If not how do I set it all up. ...
9
1909
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.