当不使用字符串操作时,GCC 11 给出 -Wstringop-overflow

Ton*_*tes 7 c arrays gcc struct function

这是我的代码。

\n
// test.c\n#include <stdio.h>\n\n#define ARRAY_SIZE 4\n\nstruct example_t {\n  int field0;\n  int field1;\n  int field2;\n  int field3;\n  int field4;\n};\n\nstruct example_t func(int *in, int array[ARRAY_SIZE]) {\n  struct example_t out;\n  return out;\n}\n\nint main() {\n  int array[ARRAY_SIZE] = { 0, 0, 0, 0 };\n  int a = 0;\n  struct example_t col = func(&a, array);\n  return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

gcc11.1.0 给了

\n
$ gcc test.c -o test\ntest.c: In function \xe2\x80\x98main\xe2\x80\x99:\ntest.c:22:26: warning: \xe2\x80\x98func\xe2\x80\x99 accessing 16 bytes in a region of size 4 [-Wstringop-overflow=]\n   22 |   struct example_t col = func(&a, array);\n      |                          ^~~~~~~~~~~~~~~\ntest.c:22:26: note: referencing argument 2 of type \xe2\x80\x98int *\xe2\x80\x99\ntest.c:14:18: note: in a call to function \xe2\x80\x98func\xe2\x80\x99\n   14 | struct example_t func(int *in, int array[ARRAY_SIZE]) {\n      |                  ^~~~\n
Run Code Online (Sandbox Code Playgroud)\n

g++没有。

\n

我不明白为什么会出现警告消息,因为我的代码中没有字符串操作,而且我从未array读过func.

\n

如果 中只有 4 个或更少的字段struct example_t,GCC 不会抱怨。有人可以解释一下为什么这条消息在这里以及我该如何修复它吗?

\n

先感谢您。

\n