Sto*_*row 0 c gcc posix pid default-value
打开-Wextra标志gcc似乎具有禁止structs部分初始化的效果。例如:
// main.c
#include <pthread.h>
typedef struct S {
int i;
pid_t pid;
} S;
int main( int argc, char* argv[] ) {
(void)argc;
(void)argv;
S s = { 42 };
(void)s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ gcc --version && gcc -Wall -Wextra -pedantic -Werror ./main.c
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
./main.c: In function ‘main’:
./main.c:12:3: error: missing initializer for field ‘pid’ of ‘S {aka struct S}’ [-Werror=missing-field-initializers]
S s = { 42 };
^
./main.c:6:9: note: ‘pid’ declared here
pid_t pid;
^~~
cc1: all warnings being treated as errors
Run Code Online (Sandbox Code Playgroud)
a 是否有任何合理的初始(无效)值pid_t?我一直觉得,既然pid_t是不透明的,就不应该对标志值做出任何假设。
如果 a 没有合理的初始/无效值,pid_t 是否还有其他一些好的/广泛使用的编程实践不会遇到此错误?
注意:特别有兴趣了解是否存在不涉及分配任意值的选项,该值在单独的有效性标志变量设置为真之前被假定为无效,例如
typedef struct S {
int i;
pid_t pid;
bool valid;
} S;
S s = { 42, 9, false };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |