in overloading problem reference against pointer

J

josh

Why if I use:
Array *Array::eek: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::eek: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
 
R

Rolf Magnus

josh said:
Why if I use:
Array *Array::eek:perator=( const Array *right )

That's quite an unusual assignment operator. Why don't you use references?
{
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

Do you ensure that ptr is always either a valid pointer or a null pointer?
Otherwise, delete [] might choke.
and if I use the same codebut with references arguments I have not
that error:
Array &Array::eek:perator=( const Array &right )
{
if ( right != this )

You probably mean:

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;
}
 
J

josh

That's quite an unusual assignment operator. Why don't you use references?

Oh yes I do it only for testing purpose and to see how the compiler
"answer me"
{
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

Do you ensure that ptr is always either a valid pointer or a null pointer?
Otherwise, delete [] might choke.
and if I use the same codebut with references arguments I have not
that error:
Array &Array::eek:perator=( const Array &right )
{
if ( right != this )

You probably mean:

if ( &right != this )
yes
but why in "the pointer case" I have that error? it seems the same
error we can
have when we try to assign an object to another and the one contains a
dynamic variable
allocated ...
 
J

josh

Oh I found the error.

If I use a pointer or a reference notations nothing is changing but if
in my code I write (and in fact I wrote so...)

a1 = a2

than the compiler try to find a function in which there is an argument
of type Array or
Array& and if not than it make a deep-copy of a2 members in a1 members
and as in
a2 there is an object of type int* then it generates that errors as
both have the same
allocated memory and when a1 or a2 go out of the main the delete[] in
the destructor
function is called twice........

to resolve we MUST write

a1 = &a2

sorry for my "didn't see" ERROR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top