Re: Invalid pointer dereference, or not?
loose AT astron DOT nl <loose@astron.nl> writes:
I was quite baffled to see this (simplified) program run without
segfaults, and without valgrind complaining about invalid memory
reads.
There is no invalid memory read that valgrind and similar tools could
detect.
<code>
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
void print() const { cout << "Hello World" << endl; }
};
int main()
{
A* a;
a->print(); // Should segfault, shouldn't it?
The ISO C++ Standard doesn't define what a segmentation fault is and
when it should happen. What it does mention is "undefined behavior",
and reading an uninitialized variable is one of the situations that
cause a program to have undefined behavior.
What that means for a particular execution of the program is, well,
undefined. The program may seem to work, it can crash, the computer
can catch fire, etc.
a = new A();
a->print();
delete a;
a->print(); // Should segfault, shouldn't it?
Same thing here.
return 0;
}
</code>
Is this valid/correct C++?
No.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]