Home > Software engineering >  Clang warning on delete pointers
Clang warning on delete pointers

Time:09-30

I begin to use clang to replace gcc. But when I delete[] pointers, it gives warning. But when I change, the warning disappears. Why and how to deal with that?

int *a = new int[1];
int *b = new int[1];
delete[] a, b;
a.cpp:7:17: warning: expression result unused [-Wunused-value]
    delete[] a, b;
int *a = new int[1];
int *b = new int[1];
delete[] a;
delete[] b;

no warning.

CodePudding user response:

delete[] a, b;

is parsed as:

(delete[] a), (b);

Which you can really think of as:

delete[] a;
b;

In which case it is pretty clear that you're not doing much with b.

Where's the warning with GCC?

If you use -Wall, gcc will also warn on this since atleast 2007 (gcc 4.1.2):

<source>: In function 'int main()':
<source>:4:18: warning: right operand of comma operator has no effect [-Wunused-value]
    4 |     delete[] a, b;
      |                  ^
Compiler returned: 0
  • Related