In this reading, you will learn about the organization of struct
s in memory. While struct
s are more complicated than arrays, they share some similarities in terms of memory organization. You’ll also be introduced to the concept of data alignment, whose importance will become more obvious when we begin discussion of caches.
This reading also contains a brief introduction to pointer arithmetic, which is an alternative to using array notation.
Sections 3.9.1 and 3.9.3 (pp. 265-269, 273-275) from the course textbook.
Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.
struct
and (optionally) a set of alignment restrictions, give the offsets of each field in the struct
.struct
into the equivalent x86-64 instruction, given the starting address of the struct (e.g. %rdx
) and the location of the value you are copying into the field (e.g. %rax
).->
) operator and one that does not.The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
->
” operator to access fields of a struct to which you have a pointer.These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.
Consider the following struct
definition.
struct TaylorSwift {
char *p;
int x;
char c;
int y;
};
For each of the fields, give its offset from the beginning of the struct assuming the recommended Intel x86-64 data alignments.
p
:x
:c
:y
:Assume that we have an array of TaylorSwift
structs. Will there need to be any “padding” at the end of each element in the array, again assuming the recommended Intel alignments?
Consider the following C code.
struct TaylorSwift ts;
ts.y = 1989;
Translate the assignment statement into an equivalent x86-64 instruction, assuming that the starting address of ts
is stored in %rbx
.
Consider the following C code:
struct TaylorSwift *tsp = malloc(sizeof(struct TaylorSwift));
(*tsp).x = 7;
Convert the statement in the second line to an equivalent statement that uses the arrow (->
) operator.