ASSERT(pointer);
pointer->x;
In this code, the ASSERT seems to be redundant. If the pointer is NULL, pointer->x will fail anyway. Is my argument correct?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The important (if not main) purpose of assertions is to document the invariants that are supposed to hold at certain point in the code. The fact that
assertcan also abort the program if the invariant is broken is just icing on the cake, albeit a very useful one. I’d say that in a typical program 90% of assertions are assertions that rather obviously can’t fail and never will fail. In other words,assertis to a large degree a kind of formalized comment language. Formalized in a sense that these “comments” are written in the same language the rest of the code is written in (C/C++), as opposed to plain English.In your code sample the assertion is there to tell you that the pointer is not supposed to be null here. That’s why it is there. In that sense this
assertis not redundant.As far as the execution flow is concerned,
assertis always redundant, which is why assertions are typically not compiled in the release version of the code. There’s nothing to prevent you from keeping the assertions in release code as well, but normally it is done by introducing a special kind of “release assertion”. In any case, making the main functionality of the code depend in the actions taken by an assertion is not a good programming practice. Assertions are supposed to be redundant, as far as the main functionality of the code is concerned.