|
From: Bob W. <bo...@ph...> - 2002-04-08 16:00:15
|
I will appologize in advance for the length of this message. Anyone who =
has much experience in programming knows pointers are an extremely =
powerful and useful construct, but that they can be the quickest path to =
the hellfire and damnation of runtime erros and program crashes if =
careful attention is not paid to how they are used.=20
With that as a backdrop here is a quote from Sams Teach Yourself C++ in =
24 Hours, third Edition by Jesse Liberty.=20
-------------------------------------------------------------------------=
-------
When you call a delete on a pointer, the memory it points to is freed. =
Calling delete on that pointer again will crash your program! When you =
delete a pointer, set it to 0 (null). Calling delete on a null pointer =
is guaranteed to be safe. For example
Animal: *pDog =3D new Animal;
delete pDog; // frees the memory
pDog =3D 0; // sets the pointer to null
// . . .
delete pDog; // harmless
-------------------------------------------------------------------------=
-------
But it does not work that way. As a matter of fact with both the =
Bloodshed/GNU compiler supplied with the book and VC++ 6.0 it works just =
the opposite. The book uses this program to demonstrate allocating and =
deleting a pointer.=20
-------------------------------------------------------------------------=
-------
// Listing 9.4
// Allocating and deleting a pointer
#include <iostream>
int main()
{
int localVariable =3D 5;
int * pLocal=3D &localVariable;
int * pHeap =3D new int;
if (pHeap =3D=3D NULL)
{
std::cout << "Error! No memory for pHeap!!";
return 1;
}
*pHeap =3D 7;
std::cout << "localVariable: " << localVariable << "\n";
std::cout << "*pLocal: " << *pLocal << "\n";
std::cout << "*pHeap: " << *pHeap << "\n";
delete pHeap;
pHeap =3D new int;
if (pHeap =3D=3D NULL)
{
std::cout << "Error! No memory for pHeap!!";
return 1;
}
*pHeap =3D 9;
std::cout << "*pHeap: " << *pHeap << "\n";
delete pHeap;
return 0;
}
-------------------------------------------------------------------------=
-------
If you compile and run the program the output is as advertised in the =
book;
localVariable: 5
*pLocal: 5
*pHeap: 7
*pHeap: 9
However if you modify the code by setting pHeap to null and call delete =
on pHeap immedately after as shown below. The program still runs without =
error.=20
-------------------------------------------------------------------------=
-------
// Listing 9.4
// Allocating and deleting a pointer
#include <iostream>
int main()
{
int localVariable =3D 5;
int * pLocal=3D &localVariable;
int * pHeap =3D new int;
if (pHeap =3D=3D NULL)
{
std::cout << "Error! No memory for pHeap!!";
return 1;
}
*pHeap =3D 7;
std::cout << "localVariable: " << localVariable << "\n";
std::cout << "*pLocal: " << *pLocal << "\n";
std::cout << "*pHeap: " << *pHeap << "\n";
delete pHeap;
delete pHeap; // Try to crash program
pHeap =3D new int;
if (pHeap =3D=3D NULL)
{
std::cout << "Error! No memory for pHeap!!";
return 1;
}
*pHeap =3D 9;
std::cout << "*pHeap: " << *pHeap << "\n";
delete pHeap;
return 0;
}
-------------------------------------------------------------------------=
-------
If set pHeap to null, however, and then try to assign *pHeap a value, as =
shown below, the program will crash.=20
-------------------------------------------------------------------------=
-------
// Listing 9.4
// Allocating and deleting a pointer
#include <iostream>
int main()
{
int localVariable =3D 5;
int * pLocal=3D &localVariable;
int * pHeap =3D new int;
if (pHeap =3D=3D NULL)
{
std::cout << "Error! No memory for pHeap!!";
return 1;
}
*pHeap =3D 7;
std::cout << "localVariable: " << localVariable << "\n";
std::cout << "*pLocal: " << *pLocal << "\n";
std::cout << "*pHeap: " << *pHeap << "\n";
pHeap =3D 0;
*pHeap =3D33;
pHeap =3D new int;
if (pHeap =3D=3D NULL)
{
std::cout << "Error! No memory for pHeap!!";
return 1;
}
*pHeap =3D 9;
std::cout << "*pHeap: " << *pHeap << "\n";
delete pHeap;
return 0;
}
-------------------------------------------------------------------------=
-------
I have several questions.
1.. How could someone who is expert enough to write a beginning book =
on C++ make such an error
2.. Is it perhaps because the ANSI/ISO definition of the language =
and what a compiler should do has changed?=20
3.. Have GNU and the Gates crowd made similar unilateral changes to =
solve a pesky problem?
4.. Did I misread something which seems fairly straight forward?
5.. Are there other pointer inconsistances hiding in the weeds =
waiting to pounce on the unsuspecting?
|