Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Avoid
malloc()andfree(), prefer thenothrowversion ofnewwithdelete(r.10)
This warning flags places where malloc or free is invoked explicitly in accordance to R.10: Avoid malloc and free. One potential fix for such warnings would be to use std::make_unique to avoid explicit creation and destruction of objects. If such a fix isn't acceptable, operator new and delete should be preferred. In some cases, if exceptions aren't welcome, malloc and free can be replaced with the nothrow version of operators new and delete.
Remarks
To detect
malloc(), we check if a call invokes a global function namedmallocorstd::malloc. The function must return a pointer tovoidand accept one parameter of unsigned integral type.To detect
free(), we check global functions namedfreeorstd::freethat return no result and accept one parameter, which is a pointer tovoid.
Code analysis name: NO_MALLOC_FREE
See also
Example
#include <new>
struct myStruct {};
void function_malloc_free() {
myStruct* ms = static_cast<myStruct*>(malloc(sizeof(myStruct))); // C26408
free(ms); // C26408
}
void function_nothrow_new_delete() {
myStruct* ms = new(std::nothrow) myStruct;
operator delete (ms, std::nothrow);
}