Hi,
First of all, thanks for reading this.
Chapter 11 in Ivor Horton's Beginning Visual C++ 2008 is dedicated to debugging. You start off typing code exactly as presented and then as the chapter progresses the author leads you through the debugging functionality included with the Visual C++ IDE by fixing the provided code.
As part of this debugging process you take this default constructor for the Name class:
and modify it like so when including a destructor for the class:Code:// Default constructor Name::Name() { pFirstname = pSurname = "\0"; }
the destructor:Code:// Default constructor Name::Name() { pFirstname = new char[1]; pSurname = new char[1]; pFirstname[0] = pSurname[0] = '\0'; // Store null character }
The author's justification for the inclusion of the memory allocation involving the pFirstname and pSurname pointers:Code:// Destructor; release memory for the two data members Name::~Name() { delete[] pFirstname; delete[] pSurname; }
Something about that didn't seem right, so after further research I've found several sources that state that calling delete[] on a null pointer is legal and is a no-op (no effect).You also should make the default constructor work properly. If the default constructor doesn't allocate memory in the free store, you have the possibility that the destructor will erroneously attempt to delete memory that was not allocated in the free store.
Bottom line: Is the author wrong or am I misunderstanding his statement?



6Likes
LinkBack URL
About LinkBacks




