3 ms·
In my time in the embedded world we would reorder fields to deal with issues like alignment in MIPS. If you wanted to avoid this you would have to use a directi
by anitil 6y ago
In my time in the embedded world we would reorder fields to deal with issues like alignment in MIPS. If you wanted to avoid this you would have to use a directive like __attribute__((packed)). Does go allow you do do the same?
So I used to do things like this (hopefully the formatting is correct):
~/Desktop$ cat test1.c
#include <stdio.h>
#include <stdint.h>
// Original Ordering
struct stats {
uint8_t NumPosts;
uint16_t Reach;
uint8_t NumLikes;
};
// Packed struct
struct stats_packed {
uint8_t NumPosts;
uint16_t Reach;
uint8_t NumLikes;
} __attribute__((packed));
// Reordered
struct stats_reordered {
uint8_t NumPosts;
uint8_t NumLikes;
uint16_t Reach;
};
int main(){
printf("sizeof(struct stats) = %ld\n", sizeof(struct stats));
printf("sizeof(struct stats_packed) = %ld\n", sizeof(struct stats_packed));
printf("sizeof(struct stats_reordered) = %ld\n", sizeof(struct stats_reordered));
return 0;
}
~/Desktop$ gcc test1.c && ./a.out
sizeof(struct stats) = 6
sizeof(struct stats_packed) = 4
sizeof(struct stats_reordered) = 4
- hinkley 6y agoI first ran into this the other way around: misaligned 64 bit values on 64 bit MIPS (and PPC) hardware. But it's the sort of thread that once you pull on it, you just keep pulling. Getting the alignment made the data bigger, then we had to shrink it back, and while you're in there you might as well keep looking around...