how to suppress array bound warning only for a given piece of code?

Viewed 394

I want to keep all the warnings enabled so -Wno-array-bound is not an option for me.

I am using variable size array work around using a structure (zero size array in a struct) and this is causing me to run causing me warnings.

Is there an attribute method to suppress these warnings where they occur?

To reproduce the warning:

#include <iostream>

struct A
{
    int a;
    char ch[0];
};

int main()
{
    volatile A* test = (A*)malloc(sizeof(A) + (sizeof(char) * 10));
    test->a = 1;
    test->ch[0] = 'a';
}

Compile with:

g++ -O2 -Warray-bounds=2 t.cpp

warning generated:

t.cpp: In function ‘int main()’:
t.cpp:30:15: warning: array subscript 0 is above array bounds of ‘volatile char [0]’ [-Warray-bounds]
   30 |     test->ch[0] = 'a';
      |     ~~~~~~~~~~^
0 Answers
Related