0

I am updating my gcc from 4 to 10 to compile a code written with Qt and I get the following error: nonnull argument 'context' comapred to NULL [-Werror=nonnull-comapre]
The code is as follows:

bool report(const Context &context) {
 Q_ASSERT(&context)
 ...
}

This question is similar to the nonnull-compare warning for pointer to reference but the writer did not say why the compiler says this error.

  • 1
    `-Werror` options means that the compiler treats all warnings as errors. And, indeed, `Q_ASSERT(&context)` doesn't make sense. – vahancho Sep 27 '22 at 10:45
  • Why ```Q_ASSERT(&context)``` does not make sense? I would like to check if it is null or not? – Ehsan Sharifi Esfahani Sep 27 '22 at 10:57
  • 1
    `&context` can't be null; it is the location of an existing object. Thus, checking is pointless. – molbdnilo Sep 27 '22 at 10:58
  • The only way for `&context` to be a null pointer is if the code that calls this function dereferenced a null pointer. That's not allowed; the behavior of the program is undefined. `const context *ptr = nullptr; report(*context);`. Don't do that. – Pete Becker Sep 27 '22 at 13:23

0 Answers0