Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Today’s Topics Questions / comments? Midterm grading explanation Chapter 5 –storage bindings and lifetime –type checking –scope / lifetime Chapter 6 –Data types

3 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 0-4.0 scale grade coversion Your exam was graded on a curve. The total points: 90 The mean: 73.56 (highest and lowest discarded) The standard deviation: 10.35 Low: 37 High: 88 Your RawScore is the number of points you earned out of 90 (the numerator on the front of your test.) (RawScore - 73.56) EC Min(4.33, 3.0 + --------------------- + ------ ) 10.35 90 - can't get higher than 4.33.

4 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 0-4.0 scale grade coversion These are the minimum numbers for each letter grade. e.g. a 2.9 is a B a 3.53 is an A- 0.51 D - 0.84 D 1.17 D + 1.51 C- 1.84 C 2.17 C + 2.51 B- 2.84 B 3.17 B+ 3.51 A- 3.84 A 4.17 A+

5 An aside about memory Michael Eckmann - Skidmore College - CS 330 - Fall 2007 How is memory divided up and categorized for executing programs? A typical setup is as follows. I'll draw a diagram on the board. –executable code –static data area used for statically declared objects like globals and constants –stack (grows one way) used for local variable allocation during “procedure / method / subroutine / function” calls. Note: I will constantly use these four words interchangeably. –heap (grows the other way) used for dynamic objects –objects that are allocated at runtime, may change and size not known until run time e.g. linked lists with unknown number of nodes, trees with unknown number of nodes, etc.

6 Binding Storage bindings and lifetime Static variables (lifetime is the total time of execution) e.g. globals, static variables allocated in the static data area Stack-dynamic variables (what's the lifetime of these?) e.g. Local variables to methods allocated on the stack. When are they allocated and deallocated? Explicit heap-dynamic variables e.g. Variables used for data structures that shrink and grow during execution, or those only referenced through pointers or references. Can anyone give examples? allocated / deallocated on the heap Implicit heap-dynamic variables All attributes (type, size, etc.) of these are bound when value is assigned. e.g. The JavaScript example of list a few slides ago. allocated / deallocated on the heap

7 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Static variables What are some advantages and disadvantages?

8 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Static variables What are some advantages and disadvantages? + ability to have history-sensitive variables inside a method/function + efficient - addressing is direct + allocation is done before run-time so there is no run-time overhead of allocation and deallocation - reduced flexibility with only static variables you couldn't write recursive routines -why? - could waste memory can't share storage with other variables that do not have to overlap existence.

9 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Stack-dynamic variables What are some advantages and disadvantages?

10 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Stack-dynamic variables What are some advantages and disadvantages? + allows recursion + share memory space with other stack-dynamic variables - slower because bindings at runtime

11 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Explicit heap-dynamic variables (e.g. Java references to objects) What are some advantages and disadvantages?

12 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Explicit heap-dynamic variables (e.g. Java references to objects) What are some advantages and disadvantages? + flexibility / expressivity - pointers/references could be difficult to use correctly - slower at runtime b/c indirect addressing (what is indirect addressing?) - complex implementation of how these variables are stored and accessed in memory - complicated and costly heap management (e.g. Java's garbage collection)

13 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Implicit heap-dynamic variables What are some advantages and disadvantages?

14 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Evaluation of these kinds of variables. Think in terms of memory space, cost of execution, reliability, efficiency etc. Implicit heap-dynamic variables What are some advantages and disadvantages? + extremely flexible, generic code (same code works for many types) easy to write - slower - reduced error detection, therefore reduced safety

15 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Type checking --- checks that the operands of an operation are of compatible types –what does compatible type mean? If all bindings of variables to types are static then type checking can be done before run-time. If any bindings of variables to types are dynamic then type checking is required at run-time. This is Dynamic Type Checking. Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time).

16 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time). According to this definition, what would you say about Java --- is it strongly typed?

17 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time). According to this definition, what would you say about Java --- is it strongly typed? –Yes nearly. Only casting (which is explicitly done by the programmer) could cause an error to go undetected. –Also, type coercion reduces the usefulness of strong typing to some extent. What do you think I mean by this?

18 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Scope is the range of statements in which a variable is visible (which means where it can be referenced.) Static scope Static – scope of variables can be determined prior to run-time. It is a spatial relationship. –Nesting of functions/methods (p. 226) Blocks (p. 227) –Answers the question – When we reference a name of a variable which variable is it?

19 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Dynamic scope Scope of variables are determined at run-time. It is a temporal relationship, based on calling sequence. –Advantage: convenience –Disadvantage: poor readability –Also answers the question – When we reference a name of a variable which variable is it? -- but we may get a different answer vs. if the language used static scoping.

20 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Dynamic scope example from Perl: $var = 10; sub1(); # will print Yikes sub2(); # will print 10 sub sub1 { local $var = “Yikes”; sub2(); } sub sub2 { print $var. “\n”; }

21 Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Scope is typically spatial, lifetime is always temporal. They are related in many cases, but two examples where they are clearly different concepts: static variables can be declared in a function in C++ & Java. These variables are used to retain values between subsequent calls. e.g. If you wanted to know how many times you called the constructor for some class (i.e. how many instances of that class were created) during the program you could use a static variable that has one added to it each time in that constructor. What's the scope of a variable like this? What's its lifetime?

22 Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Many languages do not allow you to reference variables of a function that calls a particular function. e.g. Function1() { // do some stuff here } Function2() { int x; Function1(); } What's the scope of x? What's its lifetime?

23 Constants and initialization Michael Eckmann - Skidmore College - CS 330 - Fall 2007 What good are named constants? –Enhances readability. Anything else? Initialization –Binds a value to a variable when that variable is bound to storage (i.e. when memory is allocated.) –Occurs once for static variables –Occurs each time code is executed for dynamic variables (either stack-dynamic or heap-dynamic.)

24 Ch. 6 - Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 A Data type –defines a set of allowable values –defines the operations on those values

25 Ch. 6 - Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 How well do the types match real-world problems User defined types --- example anyone? Abstract data types --- example anyone? Structured data types –Arrays, records Data types and operations support in hardware vs. software Descriptor –collection of attributes about a var stored in memory. –Static (during compilation process) vs. dynamic (during run time) –Used for type checking and allocation & deallocation

26 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Not defined in terms of other types Used with type constructors to provide structured types Integer types –Signed vs. unsigned –Sign-magnitude vs. Two's complement vs. one's complement representation of integers. Floating point are approximations to decimal numbers. Why only approximations? Let's convert 0.1 to binary. –Fully continuous? –Precision and range.

27 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Floating point are approximations to decimal numbers. –Fully continuous? –Precision and range. single precision IEEE format: –1 sign bit, 8 bits for exponent, 23 bits for fraction double precision IEEE format: –1 sign bit, 11 bits for exponent, 52 bits for fraction

28 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 How to “fix” problems with floating point? Intervals –Interval arithmetic could guarantee that result of all operations are within the interval –Have a min and max bound on results.

29 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Decimal types –Most languages we use don't have a decimal type. –Cobol does, so does C#. –BCD –1 or 2 digits per byte –Why have decimal types if they waste storage?

30 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Decimal types –Most languages we use don't have a decimal type. –Cobol does, so does C#. –BCD –1 or 2 digits per byte –Why have decimal types if they waste storage? Because they precisely store decimal values which floating point types do not

31 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Boolean –Could be represented by a single bit, but are often represented by a byte for more efficient memory access. Character –ASCII (8 bit) –ISO 8859-1 (8 bit) –Unicode (16 bit) --- Java & c# use this 1 st 128 are ASCII

32 ASCII Michael Eckmann - Skidmore College - CS 330 - Fall 2007

33 Unicode Michael Eckmann - Skidmore College - CS 330 - Fall 2007 http://www.unicode.org/standard/WhatIsUnicode.html Has characters in alphabets of many languages

34 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Implemented as –Character array vs. primitive type –Static vs. dynamic length C & C++ strings are terminated with a null character. –Length is not maintained but can be determined. The end of the string is determined by the null character. –All that needs to be stored is a pointer to the first character in the string. –Limited dynamic length (limited because there is a maximum length)

35 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Java has two different types for Strings. –String Static length constant strings –StringBuffer Dynamic length and allows direct subscripting Fortran 95 –String is a primitive type –Has typical operations for strings Issues –What to do when assigning strings of different lengths? etc.

36 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Pattern matching with character strings –Perl, JavaScript, PHP built in pattern matching with regular expressions –Included in class libraries of C++, Java and C#

37 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Length –Static length –Limited dynamic length –Dynamic length Requires dynamic storage allocation and deallocation Maximum flexibility

38 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Implementation –Most often software (as opposed to hardware) implementation of storage, retrieval and manipulation Descriptor –Type name –Length (for static strings) –Address of first character

39 Character strings Descriptor for static strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007

40 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Descriptor for limited dynamic –Type name (Limited dynamic string) –Maximum length –Current length –Address of first character For dynamic length strings –Could be stored in linked list –Could be stored in adjacent storage cells What about when length changes? How can this be done?

41 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Linked list vs. adjacent evaluation –Linked lists string operations become complex (following pointers around) allocation and deallocation is simple and fast but requires more storage (for the pointers or references) –Adjacent faster string operations (because contiguous) less storage but allocation and deallocation is slower


Download ppt "CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google