07-processor-post3up

07-processor-post3up - The processor model Readings:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
The processor model Readings: Readings from CP:AMA, as detailed in the document available on the Web site. Vectors and arrays The processor model Costs of Scheme and C primitives CS 136 Winter 2009 07: The processor model 1 Address calculations The machine instructions implementing Scheme or C structure accessors compute an offset from the first word of memory holding the structure. Given a Scheme structure with four fields whose first word is at memory address 200, the pointer to the value of the third field is stored in memory address 208, because 208 = 200 + (3 - 1) * 4 . This computation is valid even if the 3 is replaced by a larger number. This suggests a data structure with rapid access to the i th element (unlike lists). The corresponding Scheme (R5RS) data type is vector . Many other languages, including Java, C, and C++, use the word array . CS 136 Winter 2009 07: The processor model 2 To create a vector, the memory allocator must be asked for a set of consecutive memory locations of the desired size. This means that the size of a vector is fixed at the time of creation. ( define v1 ( vector 9 2 5 blue " test " )) ( define v2 ( make-vector n 0 )) ( define v3 ( list vector ( build-list 10 add1 ))) All of these functions have running time O ( n ) , where n is the number of elements involved. CS 136 Winter 2009 07: The processor model 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Vector representation in the memory model <vector, length 5> 1000 1100 3452 924 12 ... 244 248 252 256 260 264 ... address contents <type tag info> 7 ... 3448 3452 ... myvec 248 identiFer location CS 136 Winter 2009 07: The processor model 4 The function vector-length consumes a vector and produces its length. The function vector-ref consumes a vector v and a natural number i , and produces the i th element of the vector (the numbering starts from 0). ( vector-set! v i val ) mutates the i th element of a vector v so that it becomes val , and produces ( void ) . Each of these has O (1) running time. We will justify these claimed running times later in this module, when we discuss details of the processor. CS 136 Winter 2009 07: The processor model 5 Vectors provide yet another way to implement structures. Despite the speed advantage provided by indexing, vectors play a minor role in Scheme, Lisp, and other functional languages. But arrays play a major role in C and in many other imperative languages. Arrays were the first (and pretty much only) data structure provided in most of the early programming languages (Fortran, Cobol). This is because arrays are a thin abstraction of memory itself. Arrays were carried forward as a major feature through successor imperative languages (Algol, C, Pascal, Java). CS 136 Winter 2009 07: The processor model 6
Background image of page 2
Array declarations in C The statement int a[5]; declares an array a of five integers. These occupy five consecutive words in memory (in the stack frame, if this is a local declaration).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

07-processor-post3up - The processor model Readings:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online