I wrote a function in C ++ the other day, and it occurred to me that the compiler could do much more to help me protect myself from errors. The basics of my code were like this:
void method(SomeType* p)
{
assert(p != 0);
p->something();
}
And he is called like that
SomeType p = NULL;
if (SomeCondition)
{
p = some_real_value;
}
method(p);
Obviously, p can be null at runtime, and therefore the statement that the method does not work in the debug build. My mistake.
However, it seems possible that the compiler could catch this during compilation and issued a warning that it found that it found that this statement could be violated.
, , , , NULL .
, , , , , , , , .
, , , , : -
assert(index >= 0 && index < array_size);
, , .
, , , , , , , ? - Google, , - ? , , ?