Copy Constructor (Deep Copy) c++ - c++

I am working on an assignment that calls for the writing the function implementations of the Big 3 when constructing a class with pointers. I seem to be having trouble with the copy constructor. I think I am doing the other 2 correctly.
class RealBox
{
private:
float* m_reals; // Array of Real Numbers
int m_boxsize; // number of Real Numbers in this box
public:
// Purpose: Constructs an Real-Box
// Preconditions:
// 's' is greater than 0;
// Postconditions:
// m_reals points to a dynamically allocated array of size 's'
// all elements of m_reals[] are initiallized to 'a'.
RealBox(int s, float a);
/*
* --------- Big 3 Member Functions -----------
*/
// Purpose: Destructor
// Postconditions: m_reals[] deallocated
~RealBox();
// Purpose: Operator=, performs a deep copy of 'rhs' into 'this' RealBox
// Parameters: rhs, RealBox to be copied
// Returns: *this
// Postconditions: *this == rhs
const RealBox& operator=(const RealBox& rhs);
// Purpose: Copy Constructor
// Parameters: rhs - RealBox to be copied
// Postconditions: *this == rhs
RealBox(const RealBox& rhs);
/*
* ----- Simple Accessor Operations -----
*/
// Purpose: Sets a value in the RealBox
// Parameters: 'i' location to set
// 'x' value to store
// PreConditions: 'i' is between the boundaries of the RealBox
// Postconditions: element 'i' in the RealBox is set to 'x'
void set( int i, float x);
/*
* ----- Complex Accessor Operations -----
*/
// Purpose: prints the RealBox
friend std::ostream& operator<< (std::ostream& out,
const RealBox& box);
}; // RealBox
#include "realbox.h"
#include <iostream>
using namespace std;
RealBox::RealBox(int s, float a)
{
m_reals = new float[s];
for (int i=0; i < s; i++)
{
m_reals[i] = a;
}
}
RealBox::~RealBox()
{
delete [] m_reals;
m_reals = NULL;
}
const RealBox& RealBox::operator =(const RealBox& rhs)
{
if(this != &rhs)
*this = rhs;
return(*this);
}
RealBox::RealBox(const RealBox& rhs)
{
m_reals = new float[m_boxsize];
*this = rhs.m_reals;
}
void RealBox::set(int i, float x)
{
m_reals[i] = x;
}
std::ostream& operator<< (std::ostream& out, const RealBox& box)
{
out <<"[ ";
for (int i = 0; i < box.m_boxsize; i++)
{
out << box.m_reals[i] << ", ";
}
out <<"]"<< endl;
return(out);
}
When I try to compile, I am getting the error:
realbox.cpp: In copy constructor ‘RealBox::RealBox(const RealBox&)’:
realbox.cpp:33:8: error: no match for ‘operator=’ (operand types are ‘RealBox’ and ‘float* const’)
*this = rhs.m_reals;
^
realbox.cpp:33:8: note: candidate is:
realbox.cpp:23:16: note: const RealBox& RealBox::operator=(const RealBox&)
const RealBox& RealBox::operator =(const RealBox& rhs)
^
realbox.cpp:23:16: note: no known conversion for argument 1 from ‘float* const’ to ‘const RealBox&’
I think my operator = overload is faulty as well but I'm not sure exactly what went wrong. It didn't give me that error until I started trying to fix. It's trying to say I'm comparing two different data types but I'm not sure how that is happening.

This line:
*this = rhs.m_reals;
Attempts to assign a float* to a RealBox object. The two types are incompatible.
Second, you failed to initialize m_boxsize when you constructed the object.
What you want to do is this:
RealBox::RealBox(const RealBox& rhs)
{
m_boxsize = rhs.boxsize;
m_reals = new float[m_boxsize];
for (int i = 0; i < m_boxsize; ++i )
m_reals[i] = rhs.m_reals[i];
}
This can be shortened to this:
RealBox::RealBox(const RealBox& rhs) : m_boxsize(rhs.m_boxsize),
m_reals(new float[rhs.m_boxsize])
{
std::copy(rhs.m_boxsize, rhs.m_boxsize + m_boxsize, m_reals);
}
In addition, your other constructor needs to initialize the m_boxsize variable:
RealBox::RealBox(int s, float a) : m_boxsize(s)
{
m_reals = new float[s];
for (int i=0; i < s; i++)
m_reals[i] = a;
}
This can also be shortened to this:
RealBox::RealBox(int s, float a) : m_boxsize(s), m_reals(new float[s])
{
std::fill( m_reals, m_reals + s, a);
}
The std::fill and std::copy are defined in the <algorithm> header.
Once you fix this, the assignment operator is very simple:
#include <algorithm>
//...
RealBox& RealBox::operator =(RealBox rhs)
{
std::swap(rhs.m_reals, m_reals);
std::swap(rhs.m_boxsize, m_boxsize);
return *this;
}
Usage of the copy/swap idiom makes this all possible. Simple as simple can be.
What is the copy-and-swap idiom?

sorry for short answer ...
first add a member to save the table size inside the costructor
RealBox::RealBox(int s, float a)
{
m_reals = new float[s];
for (int i=0; i < s; i++)
{
m_reals[i] = a;
}
m_realCount = s;
}
then replace :
RealBox::RealBox(const RealBox& rhs)
{
m_reals = new float[m_boxsize];
*this = rhs.m_reals; // << this is a big mistake
}
by :
RealBox::RealBox(const RealBox& rhs)
{
this->m_reals = new float[rhs.m_realCount];
memcpy(this->m_reals,rhs.m_reals,rhs.m_realCount*sizeof(float));
this->m_realCount = rhs.m_realCount;
}
You may need to include stdlib.h

Related

Problem with my Copy and Move constructor and assignment operators

I want to build my own full Vector class in C++. I started like this:
#include <iostream>
#include <initializer_list>
#define Print(x)(std::cout<< x << std::endl)
// Vector Class //
template <typename T>
class Vector
{
// Attributes
int length = 0;
T* array;
public:
// Default constructor
Vector()
: length(0), array(nullptr)
{ }
// Copy constructor
template<typename U>
Vector(const Vector<U>& other)
: Vector(other.len())
{
Print("Use Copy Constructor");
// Coppying other data to new array
array = new T[other.len()];
for (auto i=0; i < other.len(); i++)
array[i] = other[i];
}
// Move constructor
Vector(Vector<T>&& other)
: length(other.len()), array(other.array)
{
Print("Use Move Constructor");
// Deleting other array
other.length = 0;
other.array = nullptr;
}
// Constructor (does not allow uniform initialisation)
Vector(int length)
: length(length), array(new T[length])
{ }
// Constructor (with initialiser list and delegate constructor)
Vector(std::initializer_list<T> list)
: Vector((int)list.size())
{
std::uninitialized_copy(list.begin(), list.end(), array);
}
// Destructor
~Vector()
{
length = 0;
delete[] array;
array = nullptr;
}
// Function Len that returns the length
int len() const
{
return length;
}
// Operator[](int i) and its const overload
auto& operator[](int i)
{
return array[i];
}
auto& operator[](int i) const
{
return array[i];
}
// Copy assignment operator
template<typename U>
Vector& operator=(const Vector<U>& other)
{
Print("Use Copy Operator");
if (this != (Vector*)& other) {
/*
This works the same way but does not solve the problem:
Vector<typename std::common_type<T,U>::type> temp(other);
temp.swap(*this);
*/
delete[] array;
length = other.len();
array = new T[other.len()];
for (auto i = 0; i < other.len(); i++)
array[i] = other[i];
}
return *this;
}
// Move assignment opertator
Vector& operator=(Vector<T>&& other)
{
Print("Use Move Operator");
if (this != (Vector*)&other){
/*
This works the same way but does not solve the problem:
Vector<T> temp(std::move(other)); // moves the array
temp.swap(*this);
*/
delete[] array;
length = other.len();
array = other.array;
other.len() = 0;
other.array = nullptr;
}
return *this;
}
};
But if I now try to use the copy assignment operator like this:
Vector<double> double_vector = {3.4677, 3.212, 2.2, 6.3};
Vector<double> a = double_vector;
I get the following error message:
error: use of deleted function 'constexpr Vector<double>::Vector(const Vector<double>&)'
I assume that the problem lies within the copy constructor and copy assignment operator, but I sadly can't find a solution. What I find strange is that, if I comment out the move constructor and move assignment operator, the code does seem to work. This lets me think that the compiler has difficulty knowing which constructor to use. But I could also be very wrong about this.
Hope I gave enough info for an answer/push in the right direction.
A template is never a copy constructor, that is a converting constructor.
And as you have defined a bunch of other constructors, the otherwise default copy constructor will be defined as deleted.

Compiler error "Pointer being freed was not allocated" only while using a foreach loop of custom objects in C++

I have an assignment where I build my own my string class. I made several MYString objects and put them in a vector. When I access the vector through subscript operator, I have no issue. But when I traverse through the vector using foreach loop, then I get a weird error ""Pointer being freed was not allocated"
Did I mess up my copy constructor?
Is my destructor and clear() function messed up?
header file is as follows:
class MYString{
friend std::ostream& operator<<(std::ostream&, const MYString&);
friend std::istream& operator>>(std::istream&, MYString&);
private:
static const int INITIAL_CAP = 20;
char* str;
static int getLength(const char*);
int cap; //capacity of the char array, in multiples of 20
int end; // location of the null terminator
int compareTo(const MYString& rhs);
void clear(); // to manually free memory
static int requiredCap(int end);
public:
MYString();
MYString(const MYString& mystr);
MYString(const char*);
~MYString();
MYString& operator=(const MYString& rhs);
char& operator[](int index);
const char& operator[](int index) const;
int length() const;
int capacity() const;
const char* c_str();
MYString operator+(const MYString& rhs);
int operator==(const MYString& rhs);
int operator>(const MYString& rhs);
int operator<(const MYString& rhs);
};
Here is some relevant function defintions
MYString::MYString(const MYString& rhs){
*this = rhs;
}
MYString& MYString::operator=(const MYString& rhs){
if (this == &rhs){
return *this;
}
clear();
cap = rhs.cap;
end = rhs.end;
str = new char[cap];
for (int i = 0; i < end; i++){
str[i] = rhs[i];
}
str[end] = '\0';
return *this;
}
MYString::~MYString(){
clear();
}
void MYString::clear(){
cap = 0;
end = 0;
delete[] str;
str = nullptr;
}
The main method is as follows:
#include "MYString.h"
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main(){
ifstream input;
input.open("file.txt");
if (input.fail()){
cout << "File error" << endl;
}
MYString s;
vector<MYString> v;
int count = 0;
v.push_back(MYString());
int index = 0;
while (input >> s){
if (count == 2){
v.push_back(MYString());
count = 0;
index++;
}
if (count < 2){
v[index] = v[index] + s;
count++;
}
}
for (MYString& str : v){
cout << str << endl;
}
Serious problem with your constructor:
MYString::MYString(const MYString& rhs){
*this = rhs;
}
Inside the body of your object, your data is not initialized, but you dereference this and assign to it. The data members contain garbage, including the pointer. In the assignment operator, you call clear, which calls delete[] on this garbage pointer. This is undefined behavior.
Implementing a constructor in terms of assignment is always wrong. Assignment is for replacing state of an initialized object with new state, while constructors provide initial state to uninitialized members. The object's lifetime has not even started UNTIL the constructor completes. Calling functions on an object whose life has not officially started is undefined behavior.
Your copy constructor needs to allocate memory and copy the data into it, similar to what you did in your assignment operator, but not cleaning up the old state first. Also, consider using strcpy instead of a loop to copy bytes. It will be faster.

Why does bool casting is called?

Why is bool casting being called ?
The problem appears when the constructor Set result(*this) is called. I expect it to use the Copy Constructor, instead it casts *this to bool and uses it as an int for the constructor.
How to fix it to use the copy constructor?
Set Set::operator+(const Set& rhs)const
{
Set result(*this);
for (unsigned int i = 0; i < rhs.getSize(); i++)
{
result.add(rhs[i]);
}
return result;
}
Set::operator bool()const
{
return !!(*this);
}
Set::Set(size_t capacity)
{
data = new int[capacity];
size = 0;
this->capacity = capacity;
}
void Set::copy(const Set& copied)
{
size = copied.getSize();
capacity = copied.getCapacity();
if (data != nullptr)
delete[]data;
data = new int[capacity];
for (unsigned int i = 0; i < size; i++)
data[i] = copied.getAt(i);
}
Set::Set(Set& copied)
{
copy(copied);
}
Set& Set::operator=(const Set& copied)
{
if (this != &copied)
copy(copied);
return *this;
}
int& Set::getAt(unsigned int idx)const
{
if (idx < 0 || idx >= size)
throw "Invalid index\n";
return data[idx];
}
bool Set::operator !()const
{
if (size == 0)
return true;
return false;
}
The argument for your copy constructor Set::Set(Set& copied) is not a const reference. The operator Set Set::operator+(const Set& rhs)const is const so this is a const Set * and *this is a const Set. Since you cannot pass a const T to a T& argument (it would discard the const) you may not make use of the copy constructor in this context.
To fix this, make your copy constructor accept const Set & instead, just like the copy member function does :
Set::Set(const Set& copied)
// ^^^^^ Added const here
{
copy(copied);
}
Edit : Mandatory disclaimer that you should not have to write your own dynamically sized array. Use std::vector instead. It will greatly simplify your type and will probably be far safer.
You can declare casting operator explicit to avoid implicit casting to bool:
explicit operator bool() const {
...
}
And then use it like this:
Set::bool(//obj_name);

Get lost in parameter passing by reference, by value

I am trying to make a bag container. The problem that beyond me occurs when I am overloading the = operator.
Zsak& Zsak::operator =(const Zsak& a)
{
(*this).V=a.V;
(*this).elemsz=a.elemsz;
return *this;
}
with this header:
class Zsak
{
public:
Zsak (){V=new Elem[100];}
Zsak (const Zsak & a)
{
*this=a;
}
Zsak(int meret)
{
V=new Elem[meret];
}
~Zsak(){delete[] V;}
Zsak& operator -(const Zsak& b);
Zsak& operator =(const Zsak& a);
void Zsak_Ba(int e);
void Zsak_Bol(int e);
bool Uress();
int E_Hany(int e) const;
friend std::ostream& operator << (std::ostream& out,const Zsak& z);
private:
Elem *V;
int elemsz=0;
};
The Zsak_Ba puts an element into the bag;
The Zsak_Bol draws out an element from the bag;
What I observed through testing is that the address of const Zsak a identify with the address of *this .
with this context
Zsak z(5),c(5);
z.Zsak_Ba(1);
z.Zsak_Ba(1);
z.Zsak_Ba(1);
z.Zsak_Ba(2);
z.Zsak_Ba(2);
z.Zsak_Ba(2);
z.Zsak_Ba(4);
Zsak d=z;
d.Zsak_Bol(1);
cout<<z<<endl<<d;
It prints :
1 2
2 3
4 1
1 2
2 3
4 1
And what it really should print is:
1 3
2 3
4 1
1 2
2 3
4 1
What i should do to obtain this?
What i am doing wrong? And why?
Thanks a lot!
You are only copy the pointers (and not the content) in you assignment operator.
Zsak& Zsak::operator =(const Zsak& a)
{
if (this == &a) { // or std::addressof(a)
return *this;
}
delete[] V;
elemsz=a.elemsz;
V=new Elem[elemsz];
for (std::size_t i = 0; i < elemsz; ++i) {
V[i] = a.V[i];
}
return *this;
}
Additionally, I don't see where the elemsz is being updated (outside the member initialisation). I would imagine it would be in constructors.
Zsak(int meret)
{
V=new Elem[meret];
elemsz = meret;
}
And
Zsak ()
{
V=new Elem[100];
elemsz = 100;
}
It may also be worth noting that often a "copy-swap" is used to implement the assignment operator, and a full copy in the copy constructor (basically the other way around from what you have it).
The "copy-swap" would look like
Zsak (const Zsak & a) : elemsz(a.elemsz)
{
V = new Elem[elemsz];
for (int i = 0; i < elemsz; ++i) { // copy the contents
V[i] = a.V[i];
}
}
Zsak& Zsak::operator =(const Zsak& a)
{
Zsak temp(a);
std::swap(this->elemsz, temp.elemsz);
std::swap(this->V, temp.V);
return *this;
}
It incurs some overhead with on assignment to self, a test for self assignment can be added for this if needed.
Understanding that this is a task to use and implement dynamic memory, it may be best to re-factor out the Elem array and its management outside the of the main class. It is generally easier to maintain and correct bugs and issues as they arise.
If you use vector<Elem> instead of manual dynamic memory, you do not need any user-defined copy constructor or assignment operator. It will just work. This is the preferred solution. Let C++ do the work for you.

Trying to reduce memory allocations, what are the tricks to overloading operators?

I have written out my class with overloaded operators but I am trying to reduce the amount of memory allocations (as shown by valgrind on Linux). I understand that in certain instances that the copy constructor is called to make a local copy of an object for the function but I'm unsure which situations. As it stands I'm making a new object in each case so I feel that I could get away with ridding some of the "new" calls if I were to make use of the already copied pieces. Below are my operator+ and operator+= for reference.
// ---------------------------------------------------------------------------
// operator+
// Adds two Poly objects
Poly Poly::operator+(const Poly& rhs) const {
//case where rhs has more terms
if (maxExponent < rhs.maxExponent) {
Poly temp(rhs);
for (int i = 0; i <= maxExponent; i++) {
temp.polynomial[i] += polynomial[i];
}
return temp;
}
else {
Poly temp(*this);
for (int i = 0; i <= rhs.maxExponent; i++) {
temp.polynomial[i] += rhs.polynomial[i];
}
return temp;
}
}
// ---------------------------------------------------------------------------
// operator+=
// Adds and assigns two Poly objects
Poly& Poly::operator+=(const Poly& rhs) {
*this = *this + rhs;
return *this;
}
Here is my operator= in case the tricks depend on this:
// ---------------------------------------------------------------------------
// operator=
// Assigns a Poly object to another
const Poly& Poly::operator=(const Poly& other) {
if (&other != this) {
delete[] polynomial;
maxExponent = other.maxExponent;
polynomial = new int[maxExponent + 1];
for (int i = 0; i <= maxExponent; i++) {
polynomial[i] = other.polynomial[i];
}
}
return *this;
}
The technique you are looking for is called "expression templates".
Your operator+ takes two Poly& objects, and returns a should_be_added< Poly&, Poly& >. If you add again, it returns a should_be_added< should_be_added<Poly&, Poly&>, Poly& > (or possibly should_be_added< Poly&, Poly&, Poly& > if you know things commute and you prefer things to be flat, but that is extra work).
should_be_added then has a conversion-to-Poly, or Poly has an implicit should_be_added< T, U >&& constructor (with efficient move these two are equivalent). At that point, you have at compile time the complete tree of expressions you are assigning to your Poly. With lots of work and care, you can efficiently build a single output value.
A good way to start is to start with your operator+=(Poly const& o) and operator+=(Poly&& o) and similar "mutating" operators. These primitives can make writing other operators efficiently much easier.
You probably want to write a custom Poly& operator=( should_be_added<T,U>&& src ) so that it reuses any memory in the existing Poly object. An easy way to do this is to have a method in should_be_added that says Poly result( Poly&& src ), and implement operator Poly() as operator Poly() const { return result( Poly{} ); }, and the operator= is { swap( *this, src.result(std::move(*this)) ); return *this }
Now, none of this is easy -- expression templates are medium-deep template-fu. But the result can be that you can do your mathematical expressions in a natural way, and lose next to nothing.
Note that efficient move semantics should be easy for your Poly class -- just move the internal buffer and clear the source one.
My solution would be to reimplement Poly class with following idea: let's make it impossible to modify field 'polynomials' and make it shared across copies of same Poly using shared_ptr. This way we can have O(1) copy operator while operator+ is still O(n) - with possibility of O(1) in optimistic case :)
Let's also use std::vector instead of table and be careful about our public interface. In return we get:
smaller memory consumption
no code duplication
no need to implement copy constructor; default will work just fine :)
Forgive my sloppy implementation of operator<<. I left out implementation of optimistic case for operator+ as an exercise to the reader :).
Implemented using c++11, because I am not a masochist.
#include <memory>
#include <vector>
#include <initializer_list>
#include <iostream>
class Poly {
public:
Poly() : polynomial_(NULL) {}
Poly(std::initializer_list<double> il)
{
polynomial_.reset(new std::vector<double>(il.begin(), il.end()));
}
unsigned max_exp() const
{
return polynomial_ ? polynomial_->size() : 0;
}
Poly operator+(const Poly& o) const
{
const bool has_bigger_exp = max_exp() > o.max_exp();
const Poly & poly_big = has_bigger_exp ? *this : o;
const Poly & poly_small = has_bigger_exp ? o : *this;
auto * tmp = new std::vector<double>(*poly_big.polynomial_);
for (unsigned i = 0; i < poly_small.max_exp(); ++i) {
tmp->at(i) += poly_small.polynomial_->at(i);
}
Poly ret_obj;
ret_obj.polynomial_.reset(tmp);
return ret_obj;
}
Poly& operator+=(const Poly& o)
{
*this = *this + o;
return *this;
}
private:
std::shared_ptr<const std::vector<double>> polynomial_;
friend std::ostream& operator<<(std::ostream& os, const Poly& obj);
};
std::ostream& operator<<(std::ostream& os, const Poly& obj)
{
if (obj.max_exp() == 0) {
os << "0" << std::endl;
return os;
}
for (unsigned i = obj.max_exp()-1; i > 0; --i) {
double param = obj.polynomial_->at(i);
if (param != 0) {
os << param << " * x^" << i << " + ";
}
}
os << obj.polynomial_->at(0) << std::endl;
return os;
}
int main() {
Poly a = {1, 2, 3};
Poly b = {4, 5};
Poly c = a + b;
Poly d;
std::cout << a << b << c << d;
a += {1, 1};
std::cout << a;
return 0;
}
In the first case, with operator+, conceptually there's not much you can do. You'll need the temporary variable and you'll have to return it by value.
In the second case instead you implementing operator+= using operator+ and therefore making a copy which is then copied within the object itself with operator=. This is extremely inefficient.
For the above reasons, often, people prefer to implement operator+= first, and then implement operator+ as:
Poly Poly::operator+(const Poly& rhs) {
return Poly(*this) += rhs;
}
Which is the opposite of what you are doing here.