Sizeof return in a structure

Hi:

I defined the following structure:

struct ethernet_hdr {
  char destination[6];
  char source[6];
  unsigned short protocol;
};

consisting on 14 bytes. When I use
sizeof(struct ethernet_hdr) I get 16 bytes and not 14.
This would be a memory alignment problem?

Thanks;
Gus

Parents
  • Sounds like it, yes.

    Compilers are allowed to pad structures for alignment. I don't use the Keil ARM tools, but typically an embedded compiler will support a pragma or option of some sort to declare a structure as "packed", without any padding. The GCC version is "-fpack-struct".

Reply
  • Sounds like it, yes.

    Compilers are allowed to pad structures for alignment. I don't use the Keil ARM tools, but typically an embedded compiler will support a pragma or option of some sort to declare a structure as "packed", without any padding. The GCC version is "-fpack-struct".

Children
More questions in this forum