Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2013
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2013 by Ivor Horton; ISBN: 978-1-118-84571-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2013 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 September 24th, 2014, 01:30 PM
Authorized User
 
Join Date: Jan 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Found a bug in Ex8_02!

Code:
// Ex8_02.cpp
// Using a destructor to free memory
#include <iostream>          // For stream I/O
#include <cstring>           // For strlen() and strcpy()
using std::cout;
using std::endl;

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

public:

  // Function to display a message
  void showIt() const
  {
    cout << m_pMessage << endl;
  }

  // Constructor definition
  CMessage(const char* text = "Default message")
  {
    size_t length{ strlen(text) + 1 };
    m_pMessage = new char[length + 1];        // Allocate space for text
    strcpy_s(m_pMessage, length + 1, text);   // Copy text to new memory
  }

  ~CMessage();                               // Destructor prototype
};

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

int main()
{
  // Declare object
  CMessage motto{ "A miss is as good as a mile." };

  // Dynamic object
  CMessage* pM{ new CMessage{ "A cat can look at a queen." } };

  motto.showIt();            // Display 1st message
  pM->showIt();              // Display 2nd message

  delete pM;                // Manually delete object created with new
  return 0;
}
The error is:
Code:
CMessage(const char* text = "Default message")
  {
    size_t length{ strlen(text) + 1 };
    m_pMessage = new char[length + 1];        // Allocate space for text
    strcpy_s(m_pMessage, length + 1, text);   // Copy text to new memory
  }
He allocates one more byte than necessary to m_pMessage!
 
Old May 9th, 2015, 12:46 PM
Registered User
 
Join Date: May 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ex8_02

Hi
I have concerns about this program as well, since it introduces the need for copy constructors.
on the other hand, if I understand Horton, the increment you removed is needed to account for the end of string indicator.
I would still like to understand the idea Horton stresses about the copy and copied memory locations being one shared allocations , since my efforts have seemed to show they each have independent memory locations.
Since this is the takeoff point for a host of issues that seem to only be remedied by dynamic memory allocation, I would like to see a program using the default copy constructor that confirms the copy and copied memory locations being one shared allocations .
Mike
 
Old May 9th, 2015, 02:29 PM
Registered User
 
Join Date: May 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Math tutor USA

I have been teaching this way for many years and there is a remarkable difference
in a student’s ability to retain the skill when they have learned it through problem solving,
as opposed to just being directly taught the skill. Whether your child is working above, at,
or below grade level, the sessions we provide will surely challenge them and give them the tools
necessary to become the leaders in their classrooms and to
stand out among the crowd in the changing job force.
Math tutor USA
 
Old May 9th, 2015, 02:30 PM
Registered User
 
Join Date: May 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow Math tutor USA

I have been teaching this way for many years and there is a remarkable difference
in a student’s ability to retain the skill when they have learned it through problem solving, Common Core help Rancho Cucamonga
Common Core help USA
as opposed to just being directly taught the skill. Whether your child is working above, at,
or below grade level, the sessions we provide will surely challenge them and give them the tools
necessary to become the leaders in their classrooms and to
stand out among the crowd in the changing job force.





Similar Threads
Thread Thread Starter Forum Replies Last Post
IsNumeric Bug thicks VB.NET 1 June 26th, 2007 06:55 PM
Bug or no to bug learning C using VS.Net to compil tesh All Other Wrox Books 0 February 14th, 2007 01:06 PM
EX9_03.CPP "bug" found/explained Matthew Doucette BOOK: Beginning Visual C++ 6 0 April 8th, 2005 10:48 AM
Bug happens guoqi BOOK: ASP.NET Website Programming Problem-Design-Solution 0 October 13th, 2003 01:35 PM





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