Representing Arrays, Pointers - Lecture Slides | CMSC 212, Study Guides, Projects, Research of Computer Science

Material Type: Project; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Fall 2005;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-nvp-2
koofers-user-nvp-2 ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
1
CMSC 212 โ€“ F05 (lect 19)
Announcements
โ—Program #5 posted
2
CMSC 212 โ€“ F05 (lect 19)
Representing Arrays
โ—Arrays (C style)
โ€“ sequence of consecutive memory locations
โ€“ address of array is the address of the starting point for the array
โ€“ subscript defines offset into array
โ€ข location = baseAddr + (subscript * sizeof(object))
โ€“ no further information about array stored for access at runtime
๎˜๎˜‚๎˜ƒ๎˜„๎˜…๎˜†๎˜†๎˜‡
pf3
pf4
pf5

Partial preview of the text

Download Representing Arrays, Pointers - Lecture Slides | CMSC 212 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CMSC 212 โ€“ F05 (lect 19)^1

Announcements

l Program #5 posted

CMSC 212 โ€“ F05 (lect 19)^2

Representing Arrays

l Arrays (C style)

  • sequence of consecutive memory locations
  • address of array is the address of the starting point for the array
  • subscript defines offset into array
    • location = baseAddr + (subscript * sizeof(object))
  • no further information about array stored for access at runtime

CMSC 212 โ€“ F05 (lect 19)^3

Arrays in Other Languages

Typical Implementations

l Fortran

  • sequence of logically consecutive bytes
  • array descriptor (runtime data) stores additional information

including the number of dimensions and

  • distance between array elements (for each dimension)
  • number of elements (for each dimension)
  • lower bound of array dimension (for each dimension)

l Java

  • sequence of objects
  • array descriptor (runtime data) stores
    • type information about what this is an array of
    • length array

CMSC 212 โ€“ F05 (lect 19)^4

Comparison of Representations

l Runtime Descriptors

  • require space
  • allow checking of array overflow
  • permit passing multi-dimensional arrays as parameters

CMSC 212 โ€“ F05 (lect 19)^7

Examples of Using Array Descriptors

l Can Use Additional information to Check array bounds

l Traditional C:

int strcmp(char *a, char *b) {

int i;

for (i=0; a[i] && b[i]; i++) {

if (a[i] < b[i]) return -1;

else if (b[i] < a[i]) return 1;

if (a[i] && !b[i]) return 1;

else if (b[i]) return -1;

else return 0;

CMSC 212 โ€“ F05 (lect 19)^8

With Array Descriptors

int strcmp(aRef *a, aRef *b) {

int i;

for (i=0; i < a->len && i < b->len && a->data[i] && b->data[i]; i++)

if (a->data[i] < b->data[i]) return -1;

else if (b->data[i] < a->data[i]) return 1;

if (i >= a->len || i >= b->len) {

printf("Array subscript error\n");

exit(-1);

if (a->data[i] && !(b->data[i])) return 1;

else if (b->data[i]) return -1;

else return 0;

CMSC 212 โ€“ F05 (lect 19)^9

Structures

l Store values in a sequence of memory locations

l Compiler remembers where the fields are stored

  • generates code to compute the field
    • fieldAddr = baseAddress + fieldOffset

l Example:

struct location {

int x, y;

struct location spot = { 25, 36 };

main() {

printf("%d\n", spot.x);

printf("%d\n", spot.y);

main: Load R3 R0 spot

Output R

Load R2

Load R3 R2 spot

Output R

Halt

spot: Data 25

Data 36

CMSC 212 โ€“ F05 (lect 19)^10

Objects

l Very Similar to structures

l Include additional information

  • type of the object
  • functions to invoke on the object
    • sometime might know this from type info