Ho una struttura definita come:Perché offsetof (membro) è uguale a sizeof (struct)?
struct smth
{
char a;
int b[];
};
Quando chiamo sizeof
e offsetof
su questo struct:
cout << sizeof(struct smth) << endl;
cout << offsetof(struct smth, b) << endl;
uscita è:
4
4
Come mai quando la dimensione del stuct è 4 e char usa 1 byte, l'offset dell'array int è 4? Perché c'è qualche tipo di padding? Inoltre, perché l'array int non occupa affatto spazio?
Significa "offsetof (struct smth, b)", giusto? –
@CarlNorum Sì, lo so. Modificato. – bugra
C o C++? Sono diversi. –