Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Clang warning on delete pointers

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading