Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V664. Pointer is dereferenced in the...
menu mobile close menu
Additional information
toggle menu Contents

V664. Pointer is dereferenced in the member initializer list before it is checked for null in the body of a constructor.

Apr 11 2025

The pointer is being dereferenced in the constructor member initializer list and then checked inside the constructor body for not being a null pointer. It may signal a hidden error that may stay unnoticed for a long time.

Consider a sample of incorrect code:

Layer(const Canvas *canvas) :
  about(canvas->name, canvas->coord)
{
  if (canvas)
  {
    ....
  }
}

When dereferencing a null pointer, undefined behavior occurs, i.e. normal execution of the program becomes impossible. To fix the error you should move the initialization operation into the constructor body in the code block where the pointer is known to not be equal to zero. Here is the fixed code:

Layer(const Canvas *canvas)
{
  if (canvas)
  {
    about.set(canvas->name, canvas->coord);
  }
}

This diagnostic is classified as:

You can look at examples of errors detected by the V664 diagnostic.