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

Why does this generate an error (Debug Assertion Failed)

23 views
Skip to first unread message

fl

unread,
Jun 9, 2015, 4:59:37 PM6/9/15
to
Hi,

When I try to run a snippet code from the web, it has a Debug Assertion
Failed error in MSVC. Specifically, when it is stepped into

b0=a0;

it jumps to the assign function

array &array::operator =(const array &rhs);

At line:
delete[] data;

the Debug Assertion Error pops.

I do not understand why it has such an error. Could you explain it to me?


Thanks,


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

int *data;
size_t n;
public:
array(){
data=&tmp;
}
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;

b0=a0;
return 0;
}

fl

unread,
Jun 9, 2015, 5:12:13 PM6/9/15
to
The original code snippet does not have definition

int tmp;

inside class array. I added it to make

delete[] data;

work.

It is possible that data shouldn't be used in such a way. Then, how to use
data in the class array?


Thanks,

fl

unread,
Jun 9, 2015, 5:22:48 PM6/9/15
to
Oh, I find the answer. The constructor can be:

public:
array(){
n=0;
data=new int[n];
}

Thanks,

asetof...@gmail.com

unread,
Jun 9, 2015, 10:33:23 PM6/9/15
to
fl wrote:
#I write:
class array {
int tmp;

int *data;
size_t n;
public:
array(){
data=&tmp;
}
array &array::operator =(const array &rhs);
};

array &array::operator =( array &rhs)
{
delete[] data;
#in b0=a0
#for me, the first run time error
#is here because delete
#want to free &tmp
#and that address seems not be
#one new or malloc return
0 new messages