在我的 x86_64 Linux 机器上使用 GCC 4.5.3 初始化结构时,我遇到了一个非常奇怪的问题。
有问题的代码:
struct apr_finfo_t info = { 0 };
apr_finfo_t 是相当复杂的结构。我只想说它有 17 个复杂的其他成员。
struct apr_finfo_t {
/** Allocates memory and closes lingering handles in the specified pool */
apr_pool_t *pool;
/** The bitmask describing valid fields of this apr_finfo_t structure
* including all available 'wanted' fields and potentially more */
apr_int32_t valid;
/** The access permissions of the file. Mimics Unix access rights. */
apr_fileperms_t protection;
/** The type of file. One of APR_REG, APR_DIR, APR_CHR, APR_BLK, APR_PIPE,
* APR_LNK or APR_SOCK. If the type is undetermined, the value is APR_NOFILE.
* If the type cannot be determined, the value is APR_UNKFILE.
*/
apr_filetype_e filetype;
/** The user id that owns the file */
apr_uid_t user;
/** The group id that owns the file */
apr_gid_t group;
/** The inode of the file. */
apr_ino_t inode;
/** The id of the device the file is on. */
apr_dev_t device;
/** The number of hard links to the file. */
apr_int32_t nlink;
/** The size of the file */
apr_off_t size;
/** The storage size consumed by the file */
apr_off_t csize;
/** The time the file was last accessed */
apr_time_t atime;
/** The time the file was last modified */
apr_time_t mtime;
/** The time the file was created, or the inode was last changed */
apr_time_t ctime;
/** The pathname of the file (possibly unrooted) */
const char *fname;
/** The file's name (no path) in filesystem case */
const char *name;
/** The file's handle, if accessed (can be submitted to apr_duphandle) */
struct apr_file_t *filehand;
};
现在,当使用 GCC 4.5.3 和-std=c99 -pedantic -Wextra编译这篇文章时,我看到以下警告消息:
src/switch_apr.c: In function ‘switch_file_exists’:
src/switch_apr.c:518: warning: missing initializer
src/switch_apr.c:518: warning: (near initialization for ‘info.valid’)
显然 GCC 试图初始化第一个成员,但已经阻塞了第二个成员。不使用-W / -Wextra构建时不会出现此警告。
我可以手动初始化每个成员,但这听起来很奇怪和错误。
从我从谷歌搜索中收集到的信息来看,这个初始化似乎是完全合法的,并且有关于 GCC 3 的报告它在哪里工作。但不是 GCC 4.5 或 4.1。
希望有人可以提供帮助。:)
最好的祝福,
米海