Examples include variables and the elements of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: rp->width = t; } The objects of C++ and Java are more elaborate than structures in C, in that they also associate a set of methods with an object that can be invoked to perform computation. In C, we would simply write these as ordinary functions, such as the functions area and rotate_left shown above. End As an example, consider the following structure declaration: struct rec { int i; int j; int a[3]; int *p; }; This structure contains four fields: two 4-byte int’s, an array consisting of three 4-byte int’s, and a 4-byte integer pointer, giving a total of 24 bytes: Offset Contents 0 i 4 j 8 a[0] 20 a[1] a[2] p 3.9. HETEROGENEOUS DATA STRUCTURES 155 Observe that array a is embedded within the structure. The numbers along the top of the diagram give the byte offsets of the fields from the beginning of the structure. To access the fields of a structure, the compiler generates code that adds the appropriate offset to the address of the structure. For example, suppose variable r of type struct rec * is in register %edx. Then the following code copies element r->i to element r->j: 1 2 movl (%edx),%eax movl %eax,4(%edx) Get r->i Store in r->j Since the o...
View Full Document

Ask a homework question - tutors are online