Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 1.63 KB

File metadata and controls

40 lines (29 loc) · 1.63 KB
title C26408
ms.date 07/21/2017
ms.topic conceptual
f1_keywords
C26408
helpviewer_keywords
C26408
ms.assetid 55b0706f-1107-41c1-8ad0-c9e1e86a3b8c
description CppCoreCheck rule that enforces C++ Core Guidelines R.10

C26408 NO_MALLOC_FREE

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 is not acceptable, operator new and delete should be preferred. In some cases, if exceptions are not 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 with name "malloc" or "std::malloc". The function must return a pointer to void and accept one parameter of unsigned integral type.

  • To detect free() we check global functions with names "free" or "std::free" which return no result and accept one parameter, which is a pointer to void.

See also

C++ Core Guidelines R.10

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);
}