"Ian Collins" <ian-news@hotmail.com> wrote in message
news:4h5p1gF1phdo1U5@individual.net...
NewToCPP wrote:
Hi,
Why does a C/C++ programs crash?
When there is access to a null pointer or some thing like that programs
crash, but why do they crash?
What would you suggest as an alternative?
Normally it isn't the program that crashes, but the operating
environment that terminates the program when it attempts to access
something that doesn't belong to it. The contents of address 0 for
example.
Well, consider this (untested) program.
int main()
{
int MyInt = 10;
int* MyIntP;
MyIntP = &MyInt;
MyIntP = 20; // Ooops, meant *MyIntP = 20 here
std::cout << MyIntP << std::endl;
*MyIntP = 30; // Got syntax right this time, but pointer is wrong
}
What would you suggest this program to do? Given the proper headers it
should compile and run as it is all legal code. But the programmer made
a
mistake on one line. Instead of changing the contents of a pointer to a
value, they accidently changed the pointer itself. Then they tried to
view
to the contents of the pointer, and then change the contents of the
ponter.
What is the computer supposed to do? MyIntP = 20; is legal. There may
be
cases where it's actually valid and is what was meant to do. But in this
case it's just wrong logically.