Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #70246
| From | alexo <alelvb@inwind.it> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | I'm not able to find the error |
| Date | 2019-12-04 17:54 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <qs8ock$839$1@gioia.aioe.org> (permalink) |
Hello guys,
I've written this code to get acquainted to assignment operators,
move constructors and move assignment operators, but I got a
strange behavior in my code:
I report it in its fullness because it's 123 lines:
// code starts here
#include <iostream>
using std::cout;
using std::ostream;
class Vector
{
friend ostream& operator<<(ostream& os, Vector& v)
{
for(size_t i=0; i<v.size(); i++)
os << v[i] << ' ';
return os << '\n';
}
public:
Vector(int n=5) : sz(n), data( new int[n])
{
for(size_t i=0; i < sz; i++)
data[i] = 0;
}
Vector(const Vector& r) : // the copy constructor
sz(r.size()),
data( new int[sz] )
{
for(size_t i=0; i < sz; i++)
data[i] = r.data[i];
}
Vector(const Vector&& r) // the move constructor
sz{r.size()},
data{r.data}
{ }
Vector& operator= (const Vector& r) // the copy assignment
{
delete [] data;
sz = r.size();
data = new int[sz];
for(size_t i=0; i < sz; i++)
data[i] = r.data[i];
return *this;
}
Vector& operator= (const Vector&& r) // the move assignment
{
delete [] data;
data = new int[r.size()];
data = r.data;
return *this;
}
~Vector() // the destructor
{
cout << "deleting memory of array ";
cout << *this;
delete [] data;
data = nullptr;
}
unsigned int size() const { return sz; }
int& operator[] (int i) { return data[i]; }
Vector operator+(Vector& rhs)
{
size_t maxdim = sz, mindim = sz;
if(sz < rhs.size())
{
maxdim = rhs.size();
}
else
{
mindim = rhs.size();
}
Vector temp (maxdim);
for(size_t i=0; i < mindim; i++)
temp[i] = data[i] + rhs[i];
return temp;
}
private:
size_t sz;
int *data;
};
int main()
{
Vector v1(3), v2;
cout << "v1.size() = " << v1.size() << '\n'
<< "v2.size() = " << v2.size() << "\n\n";
v1[1] = 1; v2[2] = 3; // using the index of '[]' operator
cout << "v1 = " << v1;
cout << "v2 = " << v2;
Vector s;
s = v1 + v2; // these two lines give runtime error
// Vector s = v1 + v2; // this line is ok instead
cout << "v1 + v2 = " << s << '\n';
cout << "destroying objects..." << '\n';
return 0;
}
// code ends here
Now I give a description of the error.
g++ executing this code on my gives:
v1.size() = 3
v2.size() = 5
v1 = 0 1 0
v2 = 0 0 3 0 0
deleting memory of array 0 1 3 0 0
v1 + v2 = 0 0 -1704935408 22058 0
destroying objects...
deleting memory of array 0 0 -1704935408 22058 0
free(): double free detected in tcache 2
aborted
As the title of the message says, I'm stuck.
I did my best to write it, but evidently some subtle error lays
between those lines.
If you comment the following:
Vector s;
s = v1 + v2;
and uncomment
// Vector s = v1 + v2;
the program behaves correctly.
What could it be the problem?
Thank you.
Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread
I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-04 17:54 +0100
Re: I'm not able to find the error Volker Wippel <Volker.Wippel@v2c2RemoveNoSpam.at> - 2019-12-04 18:40 +0100
Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 10:01 -0800
Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 10:06 -0800
Re: I'm not able to find the error Manfred <noname@add.invalid> - 2019-12-04 20:16 +0100
Re: I'm not able to find the error Manfred <invalid@add.invalid> - 2019-12-05 01:42 +0100
Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:45 +0100
Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:58 +0100
Re: I'm not able to find the error Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2019-12-05 04:10 +0000
Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 05:45 +0100
Re: I'm not able to find the error Manfred <noname@add.invalid> - 2019-12-05 17:29 +0100
Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-04 22:52 +0100
Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 16:47 -0800
Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:37 +0100
csiph-web