Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

in overloading problem reference against pointer


Why if I use:
Array *Array::operator=( 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::operator=( 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

josh wrote:
> Why if I use:
> Array *Array::operator=( const Array *right )

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

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::operator=( const Array &right )
> {
>     if ( right != this )

You probably mean:

     if ( &right != this )

On 16 Feb, 11:43, Rolf Magnus <ramag@t-online.de> wrote:

> josh wrote:
> > Why if I use:
> > Array *Array::operator=( const Array *right )

> 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"

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 ...
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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc