PVS-Studio V668 warning

Source: Internet
Author: User

PVS-Studio V668 warning

Reference: http://www.viva64.com/en/d/

Http://www.viva64.com/en/pvs-studio/

Warning description

V668. There is no sense in testing thepointer against null, as the memory was allocated using the 'new' operator. Theexception will be generated in the case of memory allocation error

When the pointer value returned by the new operation is compared with NULL, The PVS-Studio analyzer has detected a problem. It usually means that if the memory cannot be allocated, the program will have an unexpected way.

If the new operation fails to allocate memory, the std: bad_alloc () exception will be thrown according to the C ++ standard program. Therefore, it is meaningless to check that the pointer is null. Let's take a look at the following simple example:

MyStatus Foo ()

{

Int * p = new int [100];

If (! P)

Return ERROR_ALLOCATE;

...

Return OK;

}

The P pointer will never be equal to 0, and the function will never return the constant value ERROR_ALLOCATE. If the memory cannot be allocated, an exception occurs. We can select the simplest way to modify the code.

MyStatus Foo ()

{

Try

{

Int * p = new int [100];

...

}

Catch (const std: bad_alloc &)

{

Return ERROR_ALLOCATE;

}

Return OK;

}

However, note that the fix code shown above is very poor. Exception Handling is a completely different philosophy: they allow us to avoid many exceptions in the check and return status. We should let the exception be thrown from the Foo function and processed at a higher level. Unfortunately, it is beyond the scope of this document to discuss how to use exceptions.

Let's take a look at what such an error looks like in real life. The following is a code snippet from a real application:

// For each processor; spawn a CPU threadto access details.

HThread = new HANDLE [nProcessors];

DwThreadID = new DWORD [nProcessors];

ThreadInfo = new PTHREADINFO [nProcessors];

// Check to see if the memory allocationhappenned.

If (hThread = NULL) |

(DwThreadID = NULL) |

(ThreadInfo = NULL ))

{

Char * szMessage = new char [128];

Sprintf (szMessage,

"Cannot allocate memory"

"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 the memory cannot be allocated, the program will crash or generate an inappropriate message and handle exceptions elsewhere.

The common cause of this problem is to modify the behavior of the new operation. In the Visual C ++ 6.0 era, the new operation returns NULL to prevent errors. Later, the Visual C ++ version followed the standard to generate an exception. Remember the changes in this behavior. Therefore, if you use an old project to compile it using a modern compiler, you should pay special attention to the V668 diagnosis.

Note N1: The analyzer does not generate a warning if placement new or "new (std: nothrow) 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 use nothrownew. obj to connect to the project. In this case, the new operation will not throw an exception. For example, this function is used to drive developers and applications. For more details, refer to the new and delete operations on MSDN http://msdn.microsoft.com/en-us/library/kftdy56f42428v%vs.110%29.aspx. In this case, you only need to disable the V668 warning.

More examples

NextXMS:

PRectangle CallTip: CallTipStart (....)

{

....

Val = new char [strlen (defn) + 1];

If (! Val)

Return PRectangle ();

....

}

....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.