473,563 Members | 2,831 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_counte r_prv;
std::cout << "\n AnyClass Constructor for: " << name << '\n';
}

AnyClass(AnyCla ss const &original) : to_play_with
(original.to_pl ay_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_counte r_prv;
std::cout << "\nAnyClass Copy Constructor for: " << name << '\n';
}

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

to_play_with = other.to_play_w ith;

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::objec t_counter_prv = 0;
unsigned const &AnyClass::obje ct_counter = AnyClass::objec t_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("chees e");
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 1301
AnyClass& operator=(AnyCl ass const &other)
{
//NB: There is no name change whatsoever

to_play_with = other.to_play_w ith;

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******* ************@ne ws.indigo.ie>.. .
[snip, and doing a bit of reformattng]
AnyClass& operator=(AnyCl ass const &other)
{
//NB: There is no name change whatsoever
to_play_with = other.to_play_w ith;
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=(AnyCl ass const &other)
{
to_play_with = other.to_play_w ith;
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
1187
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 connected users. There is a nightly tape backup but I don't want to risk losing a day's work. I have noticed that when the backend has some...
26
2651
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 I've found is to create a new database and import all the objects from the corrupted file. Has anyone else found a solution to this annoying...
2
1334
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 field in the underlying query. The subform also (among various other fields) has a textbox named LineItemPrice. This is the last field in the...
8
3395
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 ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
11
1470
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" int _tmain(int argc, _TCHAR* argv) { int* p = 0;
42
2077
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, SP4). We have approx. 20 users *logged in* at any one time (over 300 userid's exist), but we have never run metrics to see how many on average are...
2
1821
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 computers and we have seen some unsettling results. In particular, after installing a Vista computer on their LAN, one of our customer's experienced...
15
1543
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. Desmond.
9
1912
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 like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
7664
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.