structure members alignment

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello friends,
this is my first post, i dont know if i´m asking in the right place... anyway...

I got a structure in my program something like

typedef struct
{
unsigned char x;
int a;
} str;

when i use sizeof(str) i got 3, which is fine 1 for the char and 2 for the int.
but in a program for a 32 bit machine it will add padding data in order to get the rigth alignment.
I want to know if data alignment its unnecesary on 8 bits micros beacause the processor gets 1 byte at a time, or in what cases its usefull??
thanks for any help,
sorry for my english I'm from Colombia.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I want to know if data alignment its unnecesary on 8 bits micros beacause the processor gets 1 byte at a time
Yes.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Quote:
I want to know if data alignment its unnecesary on 8 bits micros beacause the processor gets 1 byte at a time
Yes.

thanks, so the sizes of the structures will always be the sum of the sizes of its members???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That depends on what its members are. When using bit fields this might not be true.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For GCC, you can use the "-fpack-struct" compile flag to pack all structure members regardless, or add "__attribute__((packed))" to a particular struct to only pack that struct as tightly as possible.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But such options have no effect. As you have already found the elements are packed to byte boundaries anyway, However if you do use an option like -pack-struct then take the same source/Makefile to an ARM/PC/other 32/64 bit then you should get the same alignment there. Without the options things will likely pack to 32bit boundaries as that is what ARMs and things are good at fetching (they have to try hard to get single bytes unlike AVRs).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks friends, i really apreciate your comments

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With avr-gcc, we can not change the alignment at all.
It is always equal to one.
https://www.avrfreaks.net/index.p...