# V668. Possible meaningless check for null, as memory was allocated using 'new' operator. Memory allocation will lead to an exception.

The analyzer has detected an issue when the value of the pointer returned by the 'new' operator is compared to zero. It usually means that the program will behave in an unexpected way if memory cannot be allocated.

If the 'new' operator has failed to allocate memory, the exception std::bad_alloc() is thrown, according to the C++ standard. It's therefore pointless to check the pointer for being a null pointer. Take a look at a simple example:

    MyStatus Foo()
    {
      int *p = new int[100];
      if (!p)
        return ERROR_ALLOCATE;
      ...
      return OK;
    }

The 'p' pointer will never equal zero. The function will never return the constant value ERROR_ALLOCATE. If memory cannot be allocated, an exception will be generated. We may choose to fix the code in the simplest way:

    MyStatus Foo()
    {
      try
      {
        int *p = new int[100];
        ...
      }
      catch(const std::bad_alloc &)
      {
        return ERROR_ALLOCATE;
      }
      return OK;
    }

Note, however, that the fixed code shown above is very poor. The philosophy of exception handling is quite different: it is due to the fact that they allow us to avoid numerous checks and returned statuses that exceptions are used. We should rather let the exception leave the 'Foo' function and process it somewhere else at a higher level. Unfortunately, discussion of how to use exceptions lies outside the scope of the documentation.

Let's see what such an error may look like in real life. Here's a code fragment taken from a real-life application:

    // For each processor; spawn a CPU thread to access details.
    hThread = new HANDLE [nProcessors];
    dwThreadID = new DWORD [nProcessors];
    ThreadInfo = new PTHREADINFO [nProcessors];
    // Check to see if the memory allocation happenned.
    if ((hThread == NULL) ||
        (dwThreadID == NULL) ||
        (ThreadInfo == NULL))
    {
      char * szMessage = new char [128];
      sprintf(szMessage,
              "Cannot allocate memory for "
              "threads and CPU information structures!");
      MessageBox(hDlg, szMessage, APP_TITLE, MB_OK|MB_ICONSTOP);
      delete szMessage;
      return false;
    }

The user will never see the error message window. If memory cannot be allocated, the program will crash or generate an inappropriate message, having processed the exception in some other place.

A common reason for issues of that kind is a change of the 'new' operator's behavior. In the times of Visual C++ 6.0, the 'new' operator is return NULL in case of an error. Later Visual C++ versions follow the standard and generate an exception. Keep this behavior change in mind. Thus, if you are adapting an old project for building it by a contemporary compiler, you should be especially attentive to the V668 diagnostic.

Note N1. The analyzer will not generate the warning if placement new or "new (std::nothrow) T" is used. For example:

    T * p = new (std::nothrow) T; // OK
    if (!p) {
      // An error has occurred.
      // No storage has been allocated and no object constructed.
      ...
    }

Note N2. You can link your project with nothrownew.obj. The 'new' operator won't throw an exception in this case. Driver developers, for instance, employ this capability. For details see: new and delete operators. Just turn off the V668 warning in this case.

References:

- Wikipedia. Placement syntax.
- Microsoft Support. Operator new does not throw a bad_alloc exception on failure in Visual C++.
- Stack Overflow. Will new return NULL in any case?

This diagnostic is classified as:

CWE-570 CWE-571 CERT-MEM52-CPP

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