in overloading problem reference against pointer

Discussion in 'C++' started by josh, Feb 16, 2007.

  1. josh

    josh Guest

    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
     
    josh, Feb 16, 2007
    #1
    1. Advertising

  2. josh

    Rolf Magnus Guest

    josh wrote:

    > 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;
    > }
     
    Rolf Magnus, Feb 16, 2007
    #2
    1. Advertising

  3. josh

    josh Guest

    On 16 Feb, 11:43, Rolf Magnus <> wrote:
    > josh wrote:
    > > Why if I use:
    > > Array *Array::eek:perator=( 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"

    >
    >
    >
    > > {
    > > 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 ...
     
    josh, Feb 16, 2007
    #3
  4. josh

    josh Guest

    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
     
    josh, Feb 16, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,413
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    451
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    431
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    580
    John J. Lee
    Sep 30, 2005
  5. A
    Replies:
    7
    Views:
    639
Loading...

Share This Page