String operation destrcutor problem

CustomString.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include <iostream>
#include <stdlib.h>
class CustomString
{
private:
	char *data;
public:
	CustomString(char*);
	~CustomString(void);
	CustomString operator+(CustomString&);
	void operator<<(std::ostream&);
};




CustomString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "CustomString.h"

using namespace std;
CustomString::CustomString( char* foo)
{
	int i=strlen(foo);
	this->data= new char[i+1];
	strcpy(this->data,foo);

	//cout<<this->data;
}

CustomString::~CustomString(void)
{
        //error occurs if uncooment following statement
	//delete[] this->data;
}

CustomString CustomString::operator+(CustomString &st2)
{
	//cout<<this->data<<"+"<<st2.data;
	char* lastStr=new char[strlen(this->data)+strlen(st2.data)+1];

	strcpy(lastStr,this->data);
	strcat(lastStr,st2.data);
	
	CustomString st3=CustomString(lastStr);
	delete[] lastStr; //delete dynamically allocated char array

	return st3; 
}

void CustomString::operator<<(ostream &os)
{
	os<<"Print"<<endl;
	os<<this->data<<endl;

}


main.cpp



1
2
3
4
5
6
7
8
9
10
11
12
#include "CustomString.h"

using namespace std;

void main()
{

		CustomString s1="FOOBARS sadaw ";
		CustomString s2="BAFBAZ sdaw";
		CustomString s3=s1+s2;
		s3<<cout;
}


Running this program shows error Debug assertion has failed. Expression BLOCK_TYPE_IS_VALID. why is it happening and how to fix it? please check the destructor it is happening if i change this.

Last edited on
You need the operator= as well, otherwise you copy the pointer (which is then freed twice) instead of the content
s3 is initialized with st3. This might call copy constructor. You don't define that (nor copy assignment). Therefore, compiler generates them for you.

What would be the implications of this?
1
2
3
4
CustomString::CustomString( const CustomString & rhs )
 : data( rhs.data )
{
}


Hence, a Rule of Three: when class manages memory, you should implement all three: dtor, copy ctor, and copy assignment.
Please correct me if i am wrong. if i make CustomString::operator+() to return by pointer or reference copy constructor will not be involved. so i only need to take care only of assignment operator.

thanks to both of you though.
if i make CustomString::operator+() to return by pointer or reference copy constructor will not be involved. so i only need to take care only of assignment operator.


If you make CustomString::operator+ return by pointer or reference, what will it be pointing or referring to? One of the objects fed to operator+ that should not be changed? A local-to-operator+ variable that will cease existing when the function returns?

In the main you've supplied there is no use of the assignment operator.
@cire I can see your point now.



i am surely misunderstanding something in this part.


In the main you've supplied there is no use of the assignment operator.

Then why coder777 asked me to implement operator=()?
so i only need to take care only of assignment operator.
nobody will use this code in year 2000, so we only need to store the two last digits of the year in all dates.

No. It is not ok to leave an error into the code just because no-one has so far used the class in a way that would expose the error.

http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29
Topic archived. No new replies allowed.