0% found this document useful (0 votes)
16 views

lecture_slides_06_064-structs

The document discusses arrays and structures in programming, focusing on memory allocation, access, and layout. It explains how to define and manipulate structures, including accessing members using pointers and the dot operator. Additionally, it covers generating pointers to array elements within structures and the memory offsets associated with them.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

lecture_slides_06_064-structs

The document discusses arrays and structures in programming, focusing on memory allocation, access, and layout. It explains how to define and manipulate structures, including accessing members using pointers and the dot operator. Additionally, it covers generating pointers to array elements within structures and the memory offsets associated with them.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

University of Washington

Section 5: Arrays & Other Data Structures


 Array allocation and access in memory
 Multi-dimensional or nested arrays
 Multi-level arrays
 Other structures in memory
 Data structures and alignment

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

Generating Pointer to Structure Member


struct rec {
int i; r r+4+4*idx
int a[3];
int *p; i a p
};
0 4 16 20

 Generating Pointer to int *find_a


Array Element (struct rec *r, int idx)
 Offset of each structure {
member determined return &r->a[idx];
}
at compile time

# %ecx = idx
# %edx = r
leal 0(,%ecx,4),%eax # 4*idx
leal 4(%eax,%edx),%eax # r+4*idx+4

Structures

You might also like