bool, std::byte, char8_t et char : adresse quelconque short, char16_t : adresse doit être paire wchar_t, int, long, long long : adresse doit être un multiple de leur taille float, char32_t : adresse doit être multiple de 4 double : adresse doit être multiple de 8Cette contrainte sur les types simples donne une contrainte sur les struct/class:
struct ShortDoubleEtChar { short a; // taille 2, alignement 2 -> positions 0à1 double b; // taille 8, alignement 8 -> position 8à15 (car 2,3,...,7 impossibles) char c; // taille 1, alignement 1 -> position 16 }; // alignement 8, taille 24 octets 'le plus petit multiple de 8 après 17 est 24'
struct ACLOSE_TRACK { // 104 / 8 => 13 UCHAR OperationCode : 8; UCHAR Immediate : 8; UCHAR Reserved1 : 8; UCHAR Track : 8; UCHAR Session : 8; UCHAR Reserved2 : 8; UCHAR Reserved3 : 8; UCHAR TrackNumber[2]; // 2* 8 UCHAR Reserved4[3]; // 3 * 8 UCHAR Control : 8; }; // Ta version: 8+1+7+1+1+6+8+2*8+3*8+8 = 80 // 80 / 8 => 10
Améliorer votre expérience CodeS-SourceS avec ce plugin:
https://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Date.
unsigned short nWeekDay : 3; // 1er unsigned short : utilise 3 bits, restent 15 unsigned short nMonthDay : 6; // le même unsigned short : utilise 3+6 bits, restent 7 unsigned short nMonth : 5; // le même unsigned short : utilise 3+6+5 bits, restent 2 unsigned short nYear : 8; // plus la place pour mettre 8 bits // 2ième unsigned short : utilise 8 bits, restent 8On donc besoin de 2
unsigned shortsoit 4 octets.
unsigned charà la place, en essayant on voit que ça fait aussi 4 octets. Sauf si on change l'ordre, on peut alors avoir une solution à 3 octets.
typedef struct _st { int champ_1: 6, champ_2: 6, champ_3: 1, champ_4: 1, champ_5: 2; }st;
struct Date { //taile 2 unsigned short nWeekDay : 1; unsigned short nMonthDay : 1; unsigned short nMonth : 1; unsigned short nYear : 1; unsigned short lunaire : 1; unsigned short martien : 1; unsigned short saturnien : 1; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionstruct Date { // taille ? unsigned short nWeekDay : 3; // 0à1: (3 bits) unsigned char nMonthDay : 6; // 2: (6 bits) // alignement:3 unsigned int nMonth : 5; // 4à7: (5 bits) unsigned char nYear : 8; // 8: (8 bits) // alignement:9à11 };
struct Date { //taile 2
unsigned short nWeekDay : 1;
unsigned short nMonthDay : 1;
unsigned short nMonth : 1;
unsigned short nYear : 1;
unsigned short lunaire : 1;
unsigned short martien : 1;
unsigned short saturnien : 1;
}