Can't spot source of corruption!

Discussion in 'C++' started by JKop, Oct 12, 2004.

  1. JKop

    JKop Guest

    I have some code which when compiled on my system prints:


    <output>

    AnyClass Constructor for: blah

    AnyClass Copy Constructor for: Copy of blah

    AnyClass Copy Constructor for: Copy of Copy of blah

    AnyClass Constructor for: monkey

    AnyClass Assignment Operator: monkey = Copy of blah

    AnyClass Assignment Operator: blah = Copy of Copy of blah

    AnyClass Assignment Operator: Copy of blah = ¦.C

    AnyClass Copy Constructor for: Copy of ¦.C

    AnyClass Constructor for: cheese
    blah
    Copy of blah
    monkey
    Copy of ¦.C
    cheese

    AnyClass Destructor for: cheese

    AnyClass Destructor for: Copy of ¦.C

    AnyClass Destructor for: monkey

    AnyClass Destructor for: Copy of Copy of blah

    AnyClass Destructor for: Copy of blah

    AnyClass Destructor for: blah

    </output>


    Note the line where it says "Copy of ¦.C". . . there's something corrupted
    there. I've looked through my code a few times but can't spot who what when
    where or why. Here's the code. I've highlighted with asterisks the line
    which results in the corruption.

    Here goes:

    #include <iostream>
    #include <cstddef>
    #include <cstring>

    class AnyClass
    {
    public:

    static unsigned const &object_counter;
    //The above is a const reference to simulate a read-only variable


    unsigned to_play_with;


    const char* GetName() const
    {
    return name;
    }


    private:

    static unsigned object_counter_prv;
    //The above is the actual non-const variable

    char* name;

    public:

    AnyClass(const char* const in_name, unsigned const in_to_play_with = 0)
    : to_play_with(in_to_play_with)
    {
    std::size_t length = std::strlen( in_name );

    name = new char[length += 1];

    memcpy(name, in_name, length);

    ++object_counter_prv;
    std::cout << "\n AnyClass Constructor for: " << name << '\n';
    }

    AnyClass(AnyClass const &original) : to_play_with
    (original.to_play_with)
    {
    std::size_t length = std::strlen( original.name );

    name = new char[length += 9]; //9 = 8 + 1 (1 for the null
    character)

    memcpy( name, "Copy of " , 8 ); // . . .waste of a null
    character

    memcpy( &name[8], original.name, length -= 9 ); //Take the 9
    back off

    name[length += 8] = '\0';

    ++object_counter_prv;
    std::cout << "\nAnyClass Copy Constructor for: " << name << '\n';
    }

    AnyClass& operator=(AnyClass const &other)
    {
    //NB: There is no name change whatsoever

    to_play_with = other.to_play_with;

    std::cout << "\n AnyClass Assignment Operator: " << name <<
    " = " << other.name << '\n';
    }

    ~AnyClass()
    {
    std::cout << "\n AnyClass Destructor for: " << name << '\n';

    delete [] name;
    --object_counter_prv;
    }
    };

    unsigned AnyClass::eek:bject_counter_prv = 0;
    unsigned const &AnyClass::eek:bject_counter = AnyClass::eek:bject_counter_prv;


    int main()
    {
    AnyClass blah("blah");


    AnyClass poo = blah;

    AnyClass toke = poo;


    AnyClass monkey("monkey");

    monkey = poo;

    AnyClass cow(poo = blah = toke); // **** CORRUPTION ***

    AnyClass cheese = AnyClass("cheese");


    std::cout << blah.GetName() << '\n';
    std::cout << poo.GetName() << '\n';
    std::cout << monkey.GetName() << '\n';
    std::cout << cow.GetName() << '\n'; // *** shows corruption again ***
    std::cout << cheese.GetName() << '\n';
    }



    Can anyone spot the error?


    -JKop
     
    JKop, Oct 12, 2004
    #1
    1. Advertising

  2. JKop

    JKop Guest

    Found it!


    > AnyClass& operator=(AnyClass const &other)
    > {
    > //NB: There is no name change whatsoever
    >
    > to_play_with = other.to_play_with;
    >
    > std::cout << "\n AnyClass Assignment Operator: " <<
    > name <<
    > " = " << other.name << '\n';
    > }




    What do you notice with the above?


    return *this;


    -JKop
     
    JKop, Oct 12, 2004
    #2
    1. Advertising

  3. JKop

    Guest

    JKop <> wrote in message news:<xHPad.33243$>...
    [snip, and doing a bit of reformattng]
    > AnyClass& operator=(AnyClass const &other)
    > {
    > //NB: There is no name change whatsoever
    > to_play_with = other.to_play_with;
    > std::cout << "\n AnyClass Assignment Operator: " << name <<
    > " = " << other.name << '\n';
    > }


    I'm going blind. I don't see a return here. Shouldn't there be one?
    Socks
     
    , Oct 12, 2004
    #3
  4. JKop

    Old Wolf Guest

    JKop <> wrote
    > I have some code which when compiled on my system prints:

    [garbage]
    >
    > AnyClass& operator=(AnyClass const &other)
    > {
    > to_play_with = other.to_play_with;
    > std::cout << "\n AnyClass Assignment Operator: " << name <<
    > " = " << other.name << '\n';
    > }
    >
    > int main()
    > {
    > AnyClass blah("blah");
    > AnyClass poo = blah;
    > AnyClass toke = poo;
    > AnyClass cow(poo = blah = toke); // **** CORRUPTION ***
    > }


    This is: AnyClass cow( poo.operator= ( blah.operator= (toke) ) );
    Now, what does the operator= function return?
    (You should be able to enable a warning in your compiler to
    detect this situation).
     
    Old Wolf, Oct 13, 2004
    #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. A great onestop spot

    , Feb 7, 2006, in forum: Perl
    Replies:
    0
    Views:
    2,078
  2. PerlBoy

    hot spot

    PerlBoy, Feb 22, 2006, in forum: HTML
    Replies:
    1
    Views:
    534
    Disco Octopus
    Feb 22, 2006
  3. Jim Langston
    Replies:
    10
    Views:
    513
    Richard Herring
    Dec 4, 2007
  4. RichardOnRails
    Replies:
    7
    Views:
    150
    RichardOnRails
    Apr 5, 2009
  5. J-P-W
    Replies:
    4
    Views:
    112
    Jeremy J Starcher
    Apr 16, 2009
Loading...

Share This Page