3.68 Alignment

★★★

In the following code, A and B are constants defined with #define:

typedef struct {
    int x[A][B]; /* Unknown constants A and B */
    long y;
} str1;

typedef struct {
    char array[B];
    int t;
    short s[A];
    long u;
} str2;

void setVal(str1 *p, str2 *q) {
    long v1 = q->t;
    long v2 = q->u;
    p->y = v1 + v2;
}

GCC generates the following code for setVal:

setVal:
    movslq    8(%rsi), %rax
    addq    32(%rsi), %rax
    movq    %rax, 184(rdi)
    ret

What are the values of A and B? (The solution is unique).

The offset of t in str2 is 8, and the offset of u in str2 is 32. So we know that:

4<B824<8+4+2×A324\lt B\le 8\\ 24\lt 8+4+2\times A\le 32

the offset of y in str1 is 184, so we know that:

176<4×A×B184176\lt 4\times A\times B\le 184

and we know that:

6<A104<B844<A×B466\lt A\le 10\\ 4\lt B\le 8\\ 44\lt A\times B \le 46

so, A=9 and B=5.

Last updated