Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc -Wpadded does not provide any warning

Tags:

c

gcc

I am trying to use gcc's -Wpadded option to know if gcc can help me in finding out whether a structure is padded or not. This is the following code.

#include<stdio.h>

struct my {
    char *name;
    int age;
} my_details;

int main() {
    struct my person1;
    return 0;
}

I complied the code using the following. gcc -Wpadded alignment_demo.c. It did not return any warnings. So is my structure not padded or I am missing something? man gcc however shows it supports an option called -Wpadded. Kindly help

Thanks Chidambaram

like image 495
CHID Avatar asked Apr 02 '13 11:04

CHID


People also ask

How do I show all warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.

Which option of GCC inhibit all warning messages?

-Wno-coverage-invalid-line-number can be used to disable the warning or -Wno-error=coverage-invalid-line-number can be used to disable the error. Suppress warning messages emitted by #warning directives.

How do I ignore GCC warnings?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.


1 Answers

Seems that pack struct default is - 4. (gcc -fpack-struct=4)
If it so - than this structure is already aligned.
Because

char* - 4 or 8 bytes
int - 4 bytes

If you run this, you will have warning:

azat:~$ gcc -Wpadded -fpack-struct=8 -o test /tmp/test.c
/tmp/test.c:6:1: warning: padding struct size to alignment boundary [-Wpadded]

Because of the int.

like image 166
azat Avatar answered Sep 19 '22 16:09

azat