> For the complete documentation index, see [llms.txt](https://valineliu.gitbook.io/deuterium-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/csapp-3e-homework-solution/3.-machine-level-representation-of-programs/3.68-alignment.md).

# 3.68 Alignment

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

```c
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\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\lt 4\times A\times B\le 184
$$

and we know that:

$$
6\lt A\le 10\\
4\lt B\le 8\\
44\lt A\times B \le 46
$$

so, A=9 and B=5.
