Though __attribute__ ((aligned))
works well with the typedef declaration such as :
typedef struct __attribute__((__aligned__(8))) A {
xxx ip ;
xxx udp ;
xxx ports ;
} table ;
I have come across statements which say that this is not the case with __attribute__ ((__packed__))
with typedef ! I was going through some of the related question and some of them have used the packed attribute with typedef which tallies with our code.
Now , In our code we define
typedef struct {
xxx ip ;
xxx udp ;
xxx ports ;
}__attribute__((packed)) table ;
Does the above declaration makes the compiler to silently dump the packed attribute declaration?
PS : Yes , I could have verified it ,but my circumstances are different at the moment .Lets say holidays and a smartphone !
The declaration Seems okay to me !
It can be declared in one of these ways.
#include <stdio.h>
typedef struct __attribute__((packed)) {
char old;
int ip;
int new;
} NCO;
int main(void)
{
printf("%zu", sizeof(NCO));
}
or
#include <stdio.h>
typedef struct {
char old;
int ip;
int new;
} __attribute__((packed))
int main(void)
{
printf("%zu", sizeof(NCO));
}
Ensure that the __attribute__((packed))
keyword and attribute specification immediately follows the right brace (}) of the structure declaration. If it is in any other position (such as, following a structure instance instead of preceding a structure instance), the compiler will ignore__attribute__((packed))
and issue a warning message.
Although it gives us the packed size 9
, I think it is better to avoid it as stated here and try the old school structure declaration style .