开发者

struct initialization fails with GCC 4

I have a very weird issue whilst initializing a struct with GCC 4.5.3 on my x86_64 Linux box.

Code in question:

struct apr_finfo_t info = { 0 };

apr_finfo_t is quite some complex struct. I'll just say it has 17 complex other members.

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;
};

Now, when compiling this piece with GCC 4.5.3 and -std=c99 -pedantic -Wextra, I'm se开发者_如何学JAVAeing following warning message:

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’)

Obviously GCC tries to initialize the first member, but already chokes on the second one. This warning does NOT occur when not building with -W / -Wextra.

I could initialize each member by hand but that sounds weird and wrong.

From what I could gather from a Google search, it seems this initialization is perfectly legit and there are reports for GCC 3 where it works. Not with GCC 4.5 or 4.1 though.

Hope someone can help. :)

Best regards,

Mihai


The -Wextra command-line option includes the -Wmissing-field-initializers.
Try adding -Wno-missing-field-initializers to your command-line.

$ cat 7724939.c 
#include <stdlib.h>

struct whatever {
  int a;
  int j;
  int k;
};

int main(void) {
  struct whatever x = {0};
  if (x.k) return EXIT_FAILURE;
  return 0;
}
$ gcc --version
gcc (Debian 4.6.1-4) 4.6.1
Copyright (C) 2011 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.

$ gcc -std=c99 -pedantic 7724939.c 
$ gcc -std=c99 -pedantic -Wall -Wextra 7724939.c 
7724939.c: In function ‘main’:
7724939.c:10:10: warning: missing initializer [-Wmissing-field-initializers]
7724939.c:10:10: warning: (near initialization for ‘x.j’) [-Wmissing-field-initializers]
$ gcc -std=c99 -pedantic -Wall -Wextra -Wno-missing-field-initializers 7724939.c 
$ 

Note that the warning is not required by the C Standard. It's just your compiler trying to be (too) helpful.


If you are using C++ it would be much better to include a constructor that initialises, that way initialisation would only need to be written in the constructor instead of everywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜