473,915 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string copy constructor fails

Hi,

I have a class that does not seem to work.
I cannot see the problem, and the "fix" I have found does not help me
understand what the problem was

I know I don't need a copy constructor but the class might grow later and I
prefer having my copy constructor.
This also explains why I have 2 similar function "ClearAll() and NullAll()"
they might be used later in case I have pointers.

I use the class in a vector declared
std::vector< CMyClass > m_vMyClass;
and all I do is push_back(...), erase(...) and operator[...].

// this is the original class.
class CMyClass
{
private:
std::string m_sID;
std::string m_sName;
public:
void NullAll()
{
m_sID = ""; //<== Problem 1
m_sName = ""; //<== Problem 2
}

void ClearAll()
{
m_sID = ""; //<== Problem 3
m_sName = ""; //<== Problem 4
}

~CMyClass()
{
ClearAll();
}
CMyClass()
{
NullAll();
}
CMyClass( const CMyClass&mc )
{
NullAll();
*this = mc;
}
const CMyClass& operator=(const CMyClass&mc)
{
if( this != &mc )
{
NullAll();
ClearAll();

m_sID = mc.m_sID; //<== Problem 5
m_sName = mc.m_sName; //<== Problem 6
}
return *this;
}

}

///////////////////////////////////////////////////

All the problems where causing some sort of memory assertions.

My 'Fixes' for problem 1, 2, 3 and 4 were
m_sID ="" to
m_sID.erase() and

m_sName ="" to
m_sName.erase() ;

And the fix for problem 5 and 6 were
m_sID = mc.m_sID to
m_sID = std::string( mc.m_sID.c_str( ) ); and

m_sName = mc.m_sName to
m_sName = std::string( mc.m_sName.c_st r() );

It works but I am not sure why the original code was causing a memory error.

Many thanks in advance.

Simon.
Dec 7 '05 #1
1 5250

Simon wrote:
Hi,

I have a class that does not seem to work.

I know I don't need a copy constructor but the class might grow later and I
prefer having my copy constructor.
This also explains why I have 2 similar function "ClearAll() and NullAll()"
they might be used later in case I have pointers.
That's probably a bad idea. You shouldn't think about pointer members
being 0
in a high-level class. More likely, you should think about optional
members
being not present. Also, it you use smart pointers for your members,
you get
these functions for free on the smart pointer class.
I use the class in a vector declared
std::vector< CMyClass > m_vMyClass;
and all I do is push_back(...), erase(...) and operator[...].
Ok - that seems like it can only fail if erasing an invalid iterator,
or
indexing out of bounds. Both are likely to corrupt memory.
// this is the original class.
class CMyClass
{
private:
std::string m_sID;
std::string m_sName;
public:
void NullAll()
{
m_sID = ""; //<== Problem 1
m_sName = ""; //<== Problem 2
} ....
All the problems where causing some sort of memory assertions.


Not because of those lines, but because your memory check mechanism
detected earlier corruption at that point. Fixes to those lines just
hide the bug.

HTH,
Michiel Salters

Dec 7 '05 #2

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

Similar topics

3
3125
by: Alexandros Frantzis | last post by:
Hello, I am trying to read a big file into a string. AFAIK std::string can read in words or "lines" from a stream. So one option is to continuously append all the "lines" from the file to the string. The second option is to use the istream::read(Ch *p,streamsize n) function and then create a std::string from 'buf'. However this is a waste of time and space because (if I am not mistaken) the std::string constructor will make a copy of...
24
11060
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include <string> #include <vector> #include <cstring> // presume s from somewhere, such as:
8
3006
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large buffer as I found out calling by capacity(). What if I do not need all that buffer space again want to reduce the size to a smaller one? I tried resize() passing a smaller size than the
19
6203
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
16
16455
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
24
17506
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET 2003; I am unable to upgrade to 2005 at this time, so I cannot use the newer syntax or features. ...
6
5676
by: Erik | last post by:
Hello, For many years ago I implemented my own string buffer class, which works fine except assignments - it copies the char* buffer instead of the pointer. Therefore in function calls I pass "const char*" and get the result in a parameter by reference "MyString& result". I'd like to move to nicer strings. In order to keep platform/compiler independence I consider std:string as the replacement. While googling I managed to read some...
84
15972
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
2
9248
by: suman.nandan | last post by:
Hi Experts, In the following code (sorry for using C printf in the code !) : ---------------------------------------------- #include <string> #include<cstdio> using namespace std; int main () {
0
9880
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11352
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10922
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9730
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7253
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5942
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4777
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4343
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3367
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.