I noticed that when #pragma pack is used around a struct, the alignment inside of it is not the only one that is affected, but also the alignment of the struct itself changes. Consider the following:
#include <stdio.h>
#include <stdint.h>
#pragma pack(1)
typedef struct _TEST
{
uint32_t a;
} TEST;
#pragma pack()
volatile uint8_t n;
TEST b;
int main()
{
printf("Address %lX rem %lu\n", (long unsigned int)&b, (long unsigned int)(&b)%(sizeof(int)));
return 0;
}
You can try this code is here: https://onlinegdb.com/BkebdxZEU
The program returned Address 601041 rem 1
, which means that the pragma also had an effect of aligned(1) on the struct.
Why is that? Is this a defined behavior?
b
is simply located adjacent to the transactional memory, which happens to end at404030 <__TMC_END__>
. The next address is then used:404031 <b>
.bss
layout on the specific system. Because when I change it toTEST b={1};
and thereby move the variable to.data
, the misaligned address goes away.