This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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

  • 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".

  • "typically an embedded compiler will support a pragma or option of some sort to declare a structure as 'packed'"

    In fact, I think just about any compiler will support this - Borland C++ Builder certainly does, and I think MSVC does too.

    There's no avoiding it, though - your're going to have to read the Manual to find out for sure!!