Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann.

Similar presentations


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

1 CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions / comments? Finish storage bindings (static/stack/heap)‏ Type Checking Scope / Lifetime Data Types

3 An aside about memory Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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.

4 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

5 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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?

6 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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.

7 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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?

8 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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

9 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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?

10 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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)‏

11 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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?

12 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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

13 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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 when? If any bindings of variables to types are dynamic then type checking must be done when?

14 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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.

15 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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?

16 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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 is type coercion and why does it reduce the usefulness of strong typing?

17 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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. 228) –Answers the question – When we reference a name of a variable which variable is it?

18 Scope (assume static scoping)‏ Michael Eckmann - Skidmore College - CS 330 - Fall 2008 procedure Big is X : Integer; procedure Sub1 is begin... X... end; procedure Sub2 is X : Integer; begin... X... end; begin... end;

19 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Static scope example This is allowed in C and C++ void sub() { int count; // is hidden from use inside the while loop b/c count is... // declared inside loop & that's the only one visible there while (...)‏ { int count;... count++; } Not allowed in Java or C#

20 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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.

21 Scope (assume dynamic scoping)‏ Michael Eckmann - Skidmore College - CS 330 - Fall 2008 procedure Big is X : Integer; procedure Sub1 is begin... X... // this X could the one in Big or end; // the one in Sub2 procedure Sub2 is X : Integer; begin... X... end; begin... end;

22 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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”; }

23 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2008 If dynamic scoping is allowed, when would binding of attributes of non-local variable references occur? I'll let you read up on the evaluation of Static and Dynamic scope in the text.

24 Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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?

25 Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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?

26 Constants and initialization Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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.)‏ –Why allow initialization?

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

28 Ch. 6 - Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How well do the types match real-world problems is a question you might ask when considering if a language has useful types. Does the language you're evaluating support/provide the following: –Built-in types --- those that are built into the language (e.g. the primitive types in Java)‏ –Standard types (via a standard library) --- (e.g. those like String, etc. that are provided in the Java API)‏ –User defined types --- example anyone? –Abstract data types --- example anyone? –Structured data types e.g. Arrays, records Why might we care if a language provided a type as a primitive type vs. through some standard library?

29 Ch. 6 - Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Languages maintain a descriptor at compile-time and/or run-time for each variable 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 –What is stored is only what is necessary to determine correct types, where to find the values in memory, etc. –e.g. Type Name, Address, Length/Size

30 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Not defined in terms of other types Used with type constructors to provide structured types Integer types –Signed vs. unsigned –how to store the binary representation of the integer Sign-magnitude (sign bit and rest of bits are the abs. value) --- makes arithmetic hard Two's complement (positive #'s have 1st bit 0, to negate a # invert the bits and then add 1.) --- makes arithmetic easier One's complement (to negate, invert all the bits.)‏ –problem is there are two representations for 0.

31 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Floating point numbers are approximations to decimal numbers. Why only approximations? –Fully continuous? –Precision and range. –some decimal numbers are repeating bits in binary Let's convert 0.1 to binary.

32 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

33 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 What if we added floating point versions of decimal numbers that were themselves not represented exactly in binary? (e.g..1 +.1 +.1 +...)‏

34 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How to “fix” problems with floating point? Intervals –Interval arithmetic could guarantee that the result of all operations are within the interval –We'll have a min and max bound on results. –example: [6.74345, 6.74346] might be an interval value and another might be [4.5, 4.5001] –Interval arithmetic would be defined how to add them? how to multiply them? etc. Any advantages/disadvantages vs. typical doubles? What if your language didn't have interval type, what could you do if you liked this type?

35 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Another way to "fix" the problems with floating point types is: Decimal types –Most languages we use don't have a decimal type. –Cobol does, so does C#. –BCD (Binary Coded Decimal)‏ –stores 1 or 2 digits per byte (why not 3 digits / byte?)‏ –Why have decimal types if they waste storage?

36 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Decimal types –Why have decimal types if they waste storage? Because they precisely store decimal values which floating point types do not

37 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

38 ASCII Michael Eckmann - Skidmore College - CS 330 - Fall 2008

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

40 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Implemented as –Character array vs. primitive type –Static vs. dynamic length C & C++ strings are terminated with a null character (ASCII 0). –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)‏

41 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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.

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

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

44 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

45 Character strings Descriptor for static strings Michael Eckmann - Skidmore College - CS 330 - Fall 2008

46 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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?

47 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

48 Ordinal / Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Ordinal types –Range of values are associated with positive integers Many languages provide support for user defined ordinal types like Enumerations. Enumeration types –Named constants represent positive integers –Design issues Are enumeration type values coerced to integer? –Same operations and range of values of integers Are any types coerced to enumeration type values? –Could break the allowable values of the enum type –Range of values intentionally limited.

49 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2008 e.g. In C# enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; e.g. In C++ enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; months m; // declare a variable of type months Represented as 0 to 11 typically but could be given programmer specified values in the declaration. They look the same, but C# enum types not coerced to integer whereas C++'s are --- so, C# doesn't allow operations that don't make sense, while C++ does. e.g. In C++, we can add 1 to an enum

50 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2008 e.g. In C++ enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; // declares currentMonth of type months and assigns // an initial value months currentMonth = Jun; In C++, we can add 1 to an enum –e.g. currentMonth++; Any danger here?

51 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2008 As of Java 1.5 (aka 5.0), Java does support enumerations. When used, they create full classes for the enum. See: http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html What are advantages to enumeration types?

52 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2008 What are advantages to enumeration types? –All forms of implementation of them offer better readability –Reliability No arithmetic operations on the enum types in Ada and C# --- this prevents adding/subtracting which might not make sense like in adding months together Ada and C# also restrict values within the range for the type. C treats them like integers so we don't get those advantages

53 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2008 –Reliability C++ allows numeric values to be assigned to enum types if they are explicitly cast. C++ also allows values to be assigned to enum types but the range is checked for validity. –This just checks that the integer is between the lowest and highest enum value. Enum values need not be consecutive integers. Any problem here?


Download ppt "CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google