Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What problem does this self-assignment function have?

28 views
Skip to first unread message

fl

unread,
Jun 9, 2015, 5:45:13 PM6/9/15
to

Hi,

There is an on-line post, which says that following code fails in case of
self-assignment. I run it on MSVC, but do not find any failure yet. I have
tried n=0 and n=10 two cases.

What is wrong with my comprehension on this code?


Thanks,




////////////
class array {
int tmp;

int *data;
size_t n;
public:
array(){
n=0;
data=new int[n];
}
array &array::operator =(const array &rhs);
};

array &array::operator =(const array &rhs)
{
delete[] data;

n = rhs.n;
data = new int[n];
std::copy_n(rhs.data, n, data);

return *this;
}

int main () {
array a0;
array b0;

a0=a0;
return 0;
}

Louis Krupp

unread,
Jun 9, 2015, 6:11:12 PM6/9/15
to
On Tue, 9 Jun 2015 14:44:57 -0700 (PDT), fl <rxj...@gmail.com> wrote:

>
>Hi,
>
>There is an on-line post, which says that following code fails in case of
>self-assignment. I run it on MSVC, but do not find any failure yet. I have
>tried n=0 and n=10 two cases.
>
>What is wrong with my comprehension on this code?
>
>
>Thanks,
>
>
>
>
>////////////
>class array {
> int tmp;
>
> int *data;
> size_t n;
>public:
> array(){
> n=0;
> data=new int[n];
> }
> array &array::operator =(const array &rhs);
>};
>
>array &array::operator =(const array &rhs)
>{
> delete[] data;

At this point, you can't access anything data points to, so you might
as well set it to, say, the null pointer:

data = 0;

Try self- assignment now, and see what happens.

>
> n = rhs.n;
> data = new int[n];
> std::copy_n(rhs.data, n, data);
>
> return *this;
>}
>
>int main () {
> array a0;
> array b0;
>
> a0=a0;
> return 0;
>}

Louis

fl

unread,
Jun 9, 2015, 6:49:43 PM6/9/15
to
On Tuesday, June 9, 2015 at 3:11:12 PM UTC-7, Louis Krupp wrote:
I find the failure by adding the following member function:

void array::array_set(int tmp, int number){
data[number]=tmp;
}

int main()
{
a0.array_set(3, 2);
a0=a0;
}

Then, a0.data array cannot keep its original value after self-assignment.


I am still new to C++. I noticed that data is its private data, and don't
know whether your insertion

data = 0;

is inside the member function or not. If you could give me more detail,
that is better to know other view of the problem.

Thanks,

Louis Krupp

unread,
Jun 9, 2015, 9:19:13 PM6/9/15
to
Have you tried it?

Louis

Louis Krupp

unread,
Jun 9, 2015, 10:06:29 PM6/9/15
to
(Yes, it's inside the member function.)

Louis

asetof...@gmail.com

unread,
Jun 9, 2015, 10:13:52 PM6/9/15
to
fl wrote:
>class array {
> int tmp;
>
> int *data;
> size_t n;
>public:
> array(){
> n=0;
> data=new int[n];
> }
> array &array::operator =(const array &rhs);
>};
>
>array &array::operator =(const array &rhs)
>{
> delete[] data;
>
> n = rhs.n;
> data = new int[n];
> std::copy_n(rhs.data, n, data);

If &rhs==this [as a=a;]
the problem is in copy above
because the memory is ok
but its content is miss in
delete[] data
data = new int[n];


asetof...@gmail.com

unread,
Jun 9, 2015, 10:23:58 PM6/9/15
to
I would write:
array &array::operator =(const array &rhs)
{
if(&rhs!=this)
{delete[] data;
n = rhs.n;
data = new int[n];
std::copy_n(rhs.data, n, data);
}
return *this;
}
0 new messages