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:

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

and we know that:

so, A=9 and B=5.

Last updated