Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2010
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 10th, 2011, 08:21 PM
Registered User
 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE: Example 8_05

Hello,
When I run the program it reproduces exactly the same output as in the book but before the console closes, I get corrupted heap error. Can anyone tell me why?
Here is my code:
Code:
#include <iostream>
#include <cstring>

using std::cout;
using std::endl;

class CMessage
{
private:
    char* pmessage;        // Pointer to object text string

public:
    // Function to display a message
    void ShowIt() const
    {
        cout << endl << pmessage;
    }

    ////////////////////////////////////
    // Function to reset a message to *
    void reset()
    {
        char* temp = pmessage;
        while( *temp )
            *( temp++ ) = '*';
    }

    ///////////////////////////////////////
    // Overloaded assignment operator for CMessge objects
    CMessage& operator =( const CMessage& aMess )
    {
        if( this == &aMess )    // check addresses, if equal return the 1st operand
            return *this;

        // Release memory for 1st operand
        delete[] pmessage;
        pmessage = new char[strlen( aMess.pmessage + 1 )];

        // copy 2nd operand string to 1st
        strcpy_s( this->pmessage, strlen( aMess.pmessage ) + 1, aMess.pmessage );

        // return a reference to 1st operand
        return *this;
    }
    
    ///////////////////////////////////////////////
    // Constructor definition
    CMessage( const char* text = "Default message")
    {
        pmessage = new char[strlen( text ) + 1];            // Allocate space for text
        strcpy_s( pmessage, strlen( text ) + 1, text );        // copy text to a new memory
    }

    /////////////////////////////////////////////////
    // Copy constructor definition
    CMessage( const CMessage& aMess )
    {
        size_t len = strlen( aMess.pmessage ) + 1;
        pmessage = new char[len];
        strcpy_s( pmessage, len, aMess.pmessage );
    }

    ////////////////////////////////////////////////
    // Destructor t free memory allocated by new
    ~CMessage()
    {
        cout << "Destructor called."        // just to track what happens
             << endl;
        delete[] pmessage;                    // Free memory assigned to pointer
    }
};

int main ( void )
{    
    CMessage motto1( "The God is almighty ever!" );
    CMessage motto2;
    
    cout << "motto2 contains - ";
    motto2.ShowIt();
    cout << endl;

    motto2 = motto1;        // using new assignment operator

    cout << "motto2 contains - ";
    motto2.ShowIt();
    cout << endl;

    motto1.reset();            // Setting motto1 to * doesn't
                            // affect motto2

    cout << "motto1 now contains - ";
    motto1.ShowIt();
    cout << endl;

    cout << "motto2 still contains - ";
    motto2.ShowIt();
    cout << endl;
    std::cin.get();
    return 0;
}
 
Old December 10th, 2011, 08:44 PM
Registered User
 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by GKED View Post
Hello,
When I run the program it reproduces exactly the same output as in the book but before the console closes, I get corrupted heap error. Can anyone tell me why?
Here is my code:
Code:
#include <iostream>
#include <cstring>

using std::cout;
using std::endl;

class CMessage
{
private:
    char* pmessage;        // Pointer to object text string

public:
    // Function to display a message
    void ShowIt() const
    {
        cout << endl << pmessage;
    }

    ////////////////////////////////////
    // Function to reset a message to *
    void reset()
    {
        char* temp = pmessage;
        while( *temp )
            *( temp++ ) = '*';
    }

    ///////////////////////////////////////
    // Overloaded assignment operator for CMessge objects
    CMessage& operator =( const CMessage& aMess )
    {
        if( this == &aMess )    // check addresses, if equal return the 1st operand
            return *this;

        // Release memory for 1st operand
        delete[] pmessage;
        pmessage = new char[strlen( aMess.pmessage + 1 )];

        // copy 2nd operand string to 1st
        strcpy_s( this->pmessage, strlen( aMess.pmessage ) + 1, aMess.pmessage );

        // return a reference to 1st operand
        return *this;
    }
    
    ///////////////////////////////////////////////
    // Constructor definition
    CMessage( const char* text = "Default message")
    {
        pmessage = new char[strlen( text ) + 1];            // Allocate space for text
        strcpy_s( pmessage, strlen( text ) + 1, text );        // copy text to a new memory
    }

    /////////////////////////////////////////////////
    // Copy constructor definition
    CMessage( const CMessage& aMess )
    {
        size_t len = strlen( aMess.pmessage ) + 1;
        pmessage = new char[len];
        strcpy_s( pmessage, len, aMess.pmessage );
    }

    ////////////////////////////////////////////////
    // Destructor t free memory allocated by new
    ~CMessage()
    {
        cout << "Destructor called."        // just to track what happens
             << endl;
        delete[] pmessage;                    // Free memory assigned to pointer
    }
};

int main ( void )
{    
    CMessage motto1( "The God is almighty ever!" );
    CMessage motto2;
    
    cout << "motto2 contains - ";
    motto2.ShowIt();
    cout << endl;

    motto2 = motto1;        // using new assignment operator

    cout << "motto2 contains - ";
    motto2.ShowIt();
    cout << endl;

    motto1.reset();            // Setting motto1 to * doesn't
                            // affect motto2

    cout << "motto1 now contains - ";
    motto1.ShowIt();
    cout << endl;

    cout << "motto2 still contains - ";
    motto2.ShowIt();
    cout << endl;
    std::cin.get();
    return 0;
}
Hello again,
I am sorry but i found my own error. In the function
Code:
CMessage& operator =( const CMessage& aMess)
in line:
Code:
pmessage = new char[strlen( aMess.pmessage ) + 1];
(correct version)
I initially mistyped:
Code:
pmessage = new char[strlen( aMess.pmessage + 1 )];









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.