Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 looks okay. However try to adhere to one of the following to avoid silent attribute discardation.

#include <stdio.h>
typedef struct __attribute__((packed)) {
    char old;
    int ip;
    int new;
} NCO;
int main(void)
    printf("%zu", sizeof(NCO));
#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 follow 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 shall 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 .

@Dmitry if you have to lay data out with no padding. Either to write to a set of registers or if you are reading/writing binary data to a stream/file and you know that the endianness is the same at both ends. It can also help if you are porting an software from 32bit to 64bit. If you look at the Windows SDK headers you will find them packed with similar directives for structures to ensure compatability back to year dot. – Damian Dixon Jul 8, 2020 at 15:12 The last edit seems to have introduced an error in the second snippet. The name NCO and a semicolon seems to be missing. – Ajay Brahmakshatriya Aug 3, 2021 at 19:48 Anything involving structure packing and alignment control is never really compiler-independent anyway. – Donal Fellows Aug 28, 2020 at 12:26

You are combining two statements here.

At first you define a struct with certain properties. Then you create an alias for it.

As you assign the packed-property to the alias, a already created data structure used by this alias won't be altered. (Usually you can create variables with "struct bla" and "s_bla" [created by "typedef struct bla {} s_bla"] and exchange these values among each other. If it would be possible to alter the struct by using typedef, this coherence will break.)

So: The declaration will be dumped.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.