Bonjour,

Je suis en train d'étudier l'alignement et le padding et je cherche à observer comment gcc organise les membres d'une structure en mémoire.

Voici le code que j'utilise :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct example1 {
	char c1;
	short s;
	char c2;
	int l;
};
 
struct example2 {
	char c1;
	short s;
	char c2;
	int l;
}__attribute__ ((aligned (1)));
 
struct example3 {
	char c1 __attribute__ ((aligned (4)));
	short s __attribute__ ((aligned (4)));
	char c2 __attribute__ ((aligned (4)));
	int l;
};
 
struct example4 {
	char c1;
	short s;
	char c2;
	int l;
}__attribute__ ((__packed__));
 
 
int main(){
	printf("char : %d, short : %d, long : %d\n", sizeof(char), sizeof(short), sizeof(int));
	printf("1 : %d, 2 : %d, 3 : %d, 4 : %d\n", sizeof(struct example1), sizeof(struct example2), sizeof(struct example3), sizeof(struct example4));
}
Y a-t'il un moyen de voir comment gcc organise les membres des différentes structures en mémoire ? Je me demande si, par défaut, il met 3 octets de padding après c2 ou s'il en met 1 après c2 et 2 après l (je pencherais plutôt pour la première solution).

Merci !!