Clang warning: cannot delete expression with pointer-to-'void' type A [-Wdelete-incomplete] (ext_delete_void_ptr_operand)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: cannot delete expression with pointer-to-'void' type A
Type Warning
Category Semantic Issue
Internal Id ext_delete_void_ptr_operand
Active by Default Yes
Flags -Wno-delete-incomplete (2 elements)
Internal Message cannot delete expression with pointer-to-'void' type %0
Regular Expression (?:warning|error|fatal error)\: cannot delete expression with pointer\-to\-'void' type (.*?) \[(?:\-Werror,)?\-Wdelete\-incomplete[^\]]*\]
First Commit 2010-05-24 bb3348ed33ac Downgrade deletion of a void* from an error (which is should be) to an

Description

The warning is issued by the Clang compiler when an attempt is made to delete an expression of pointer-to-'void' type using the delete operator. In C++, deleting a pointer to void (void*) is not defined behavior because the delete operator requires a complete type to invoke the appropriate destructor and to deallocate the memory correctly. A void* pointer represents an address of memory without type information, and thus, the compiler cannot determine the correct way to release the resource. This warning acts as an alert that the code may not correctly perform memory management, potentially leading to resource leaks, or in some cases, undefined behavior. The warning suggests that the type of the pointer being deleted should be a complete type rather than void*.  
AI Generated

Example

In the following example, memory is allocated for a pointer of type void* using the malloc function, which does not initialize the memory but merely allocates it. This pointer, referred to as p, is then attempted to be deleted using the C++ delete operator. The C++ delete operator is intended to deallocate memory that was allocated with the new operator, and not malloc, and it also requires a complete object type to properly destruct the object and deallocate its memory. Since void* is not a complete object type and the memory was not allocated with new, this leads to the compiler issuing a warning that the expression attempting to delete a pointer to 'void' type is not valid. The warning is an indication that the operation is not compliant with the rules of the C++ language concerning deletion of pointers and memory management.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
#include <stdlib.h>

int main() {
    void *p = malloc(10); // allocated as void*
    delete p; // issue: can't delete void* with delete
    return 0;
}
Compiler Output
<source>:5:5: warning: cannot delete expression with pointer-to-'void' type 'void *' [-Wdelete-incomplete]


Clang Internals (17.0.6)

Git Commit Message

Downgrade deletion of a void* from an error (which is should be) to an
extension warning (which other compilers seem to use). Works around a
known bug in Xalan.

llvm-svn: 104509

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaExprCXX.cpp (line 3685)

/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
/// @code ::delete ptr; @endcode
/// or
/// @code delete [] ptr; @endcode
ExprResult Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, bool ArrayForm, Expr *ExE) {
  // ...
  if (!Ex.get()->isTypeDependent()) {
    // ...
    if (Pointee->isVoidType() && !isSFINAEContext()) {
      // ...
      Diag(StartLoc, diag::ext_delete_void_ptr_operand) << Type << Ex.get()->getSourceRange();

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaCXX/new-delete.cpp

  • clang/test/SemaCXX/new-delete.cpp:174:3: warning: cannot delete expression with pointer-to-'void' type 'void *' [-Wdelete-incomplete]