J
josh
Why if I use:
Array *Array:perator=( const Array *right )
{
if ( right != this )
{ // check for self-assignment
// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right->size )
{
delete [] ptr; // reclaim space
size = right->size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right->ptr[ i ]; // copy array into
object*/
}
return this; // enables x = y = z;
}
I have a
*** glibc detected *** cpp_book_test: double free or corruption
(fasttop): 0x09b3
and if I use the same codebut with references arguments I have not
that error:
Array &Array:perator=( const Array &right )
{
if ( right != this )
{ // check for self-assignment
// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right.size )
{
delete [] ptr; // reclaim space
size = right.size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ]; // copy array into object*/
}
return *this; // enables x = y = z;
}
Thanks
Array *Array:perator=( const Array *right )
{
if ( right != this )
{ // check for self-assignment
// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right->size )
{
delete [] ptr; // reclaim space
size = right->size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right->ptr[ i ]; // copy array into
object*/
}
return this; // enables x = y = z;
}
I have a
*** glibc detected *** cpp_book_test: double free or corruption
(fasttop): 0x09b3
and if I use the same codebut with references arguments I have not
that error:
Array &Array:perator=( const Array &right )
{
if ( right != this )
{ // check for self-assignment
// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right.size )
{
delete [] ptr; // reclaim space
size = right.size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ]; // copy array into object*/
}
return *this; // enables x = y = z;
}
Thanks