lecture_slides_06_064-structs
lecture_slides_06_064-structs
Structures
University of Washington
Structures
struct rec {
int i;
int a[3];
int *p;
};
Structures
University of Washington
Structures
struct rec { Memory Layout
int i;
int a[3]; i a p
int *p; 0 4 16 20
};
Characteristics
Contiguously-allocated region of memory
Refer to members within structure by names
Members may be of different types
Structures
University of Washington
Structures
struct rec {
Accessing Structure Member int i;
int a[3];
Given an instance of the struct, we can use
int *p;
the . operator, just like Java:
};
struct rec r1; r1.i = val;
What if we have a pointer to a struct: struct rec *r = &r1;
Using * and . operators: (*r).i = val;
Or, use -> operator for short: r->i = val;
Pointer indicates first byte of structure; access members with offsets
void
set_i(struct rec *r, IA32 Assembly
int val) # %eax = val
{ # %edx = r
r->i = val; movl %eax,(%edx) # Mem[r] = val
}
Structures
University of Washington
# %ecx = idx
# %edx = r
leal 0(,%ecx,4),%eax # 4*idx
leal 4(%eax,%edx),%eax # r+4*idx+4
Structures