DOC PREVIEW
UT Arlington CSE 3302 - Data Types

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

2008-2-121CSE 3302 Programming LanguagesData Types(cont )Chengkai Li, Weimin HeSpring 2008Data Types(cont.)Lecture 8 – Data Types, Spring 20081CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Function Type in Ctypedef int (*IntFunction)(int);int square(int x) {return x*x;}IntFunction f = square;int evaluate(IntFunction g, int value){return g(value);}…printf(“%d\n”,evaluate(f,3));2Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Function Type in MLtype IntFunction = int -> int; fun square(x: int) = x * x;val f = square;Fun evaluate(g:IntFunction, value:int) = g value;…evaluate(f,3);3Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Vector, ListFunctional languages:• Vectors: like arrays, more flexibility, especially dynamic resizabilitydynamic resizability.• Lists: like vectors, can only be accessed by counting down from the first element. 4Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Pointer• A pointer type is a type in which the range of values consists of memory addresses and a special value, nil (or null)• Advantages:– Addressing flexibility (address arithmetic, explicit dereferencing and address-of, domain type not fixed (void *))–Dynamic storage management– Recursive data structures • E.g., linked liststruct CharListNode{ char data;struct CharListNode* next;};Typedef struct CharListNode* CharList;Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20085Problems with Pointers• Alias (with side-effect)int *a, *b;a=(int *) malloc(sizeof(int));*a=2;b=(int *) malloc(sizeof(int));*b=3;b=a;*b=4;printf(“%d\n”, *a);Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200862008-2-122Problems with Pointers• Dangling pointers (dangerous)int *a, *b;a = (int *) malloc(sizeof(int));*a = 1;b = a;free(a);printf(“%d\n”, *b);Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20087Problems with Pointers• Garbages (waste of memory)memory leakageint *a;a = (int *) malloc(sizeof(int));*a=2;a = (int *) malloc(sizeof(int));Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20088Type System• Type Constructors:– Build new data types upon simple data types• Type Checking: The translator checks if data types are used correctly.– Type Inference: Infer the type of an expression, whose data type is not given explicitly.e.g., x/y– Type Equivalence: Compare two types, decide if they are the same. e.g., x/y and z– Type Compatibility: Can we use a value of type A in a place that expects type B?Nontrivial with user-defined types and anonymous typesLecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20089Strongly-Typed Languages• Strongly-typed: (Ada, ML, Haskell, Java, Pascal)– Most data type errors detected at translation time– A few checked during execution and runtime error reported (e.g., subscript out of array bounds).•Pros:•Pros: – No data-corrupting errors can occur during execution. (I.e., no unsafe program can cause data errors.)– Efficiency (in translation and execution.)– Security/reliability• Cons: – May reject safe programs (i.e., legal programs is a subset of safe programs.)– Burden on programmers, may often need to provide explicit type information.Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200810Weakly-typed and untyped languages• Weakly-typed: C/C++– e.g., interoperability of integers, pointers, arrays.• Untyped (dynamically typed) languages: scheme, smalltalk, perlperl– Doesn’t necessarily result in data errors.– All type checking performed at execution time.– May produce runtime errors too frequently.Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200811Security vs. flexibility• Strongly-typed :– No data errors caused by unsafe programs.– Maximum restrictiveness, static type checking, illegal safe programs, large amount of type information supplied by programmers.• Untyped:– Runtime errors, no data-corruptions. Legal unsafe programs.– reduce the amount of type information the programmer must supply.Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008122008-2-123Security vs. flexibility• Strongly-typed :• A type system tries to maximize both flexibility and security, where flexibility means: reduce the number of safe illegal programs & reduce the amount of type information the programmer must supplyLecture 8 – Data Types, Fall 2007CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 200713programmer must supply.• Flexibility, no explicit typing or static type checkingvs.• Maximum restrictiveness, static type checkingSafe vs. LegalLegal programsProgramsLecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200814Safe programsType Equivalence• How to decide if two types are the same?• Structural Equivalence– Types are sets of valuesTwo types are equivalent if they contain the same–Two types are equivalent if they contain the same values.• Name EquivalenceLecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200815Structural Equivalence• struct RecA {char x;int y;}• struct RecB {char x;int y;Char X Intint y;}• struct RecC {char u;int v;}• struct RecD {int y;char x;}Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200816Int X CharBut are they equivalent inthese languages?• In C:struct RecA {char x; int y;};struct RecB {{char x; int y;};struct RecA a;struct RecB b;b=a;Lecture 8 – Data Types, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200817( Error: incompatible types in assignment )But are they equivalent inthese languages?• In C:struct RecA {char x; int y;};struct RecB {{char x; int y;};struct RecA a;struct RecB* b;b=&a;Lecture 8 –


View Full Document

UT Arlington CSE 3302 - Data Types

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?