Thread: [Dev-C++] Deliberate crashes
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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?
|
|
From: Ioannis V. <no...@ho...> - 2002-04-08 16:17:33
|
-----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Bob Wilson Sent: Monday, April 08, 2002 6:02 PM To: fee...@sa...; Dev-C++ User Mailing List Subject: [Dev-C++] Deliberate crashes I have several questions. How could someone who is expert enough to write a beginning book on C++ make such an error Deleting the contents of an invalid memory address (that is a memory address where there is no space allocated by new) invokes undefined behavior. Undefined behavior can be the program crashes, the OS crashes, the airplane falls, the robotic arm destroyes the machine. In your case the program does not crash but some other damage occurs (some contents of another thing are altered (e.g. of a function) or it can be that delete knows that it has erased the contents of that memory and doesn't clear them again. In any case it just *happened* to not crash your program immediately. 1. Is it perhaps because the ANSI/ISO definition of the language and what a compiler should do has changed? No. 1. Have GNU and the Gates crowd made similar unilateral changes to solve a pesky problem? It can be that some particular compiler take care of that but you can't rely on this. 1. Did I misread something which seems fairly straight forward? 2. Are there other pointer inconsistances hiding in the weeds waiting to pounce on the unsuspecting? Yes the problem may appear later. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net <http://www.noicys.cjb.net/> * Alternative URL: http://run.to/noicys |