Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 – Data Types CSCE 343.

Similar presentations


Presentation on theme: "Chapter 6 – Data Types CSCE 343."— Presentation transcript:

1 Chapter 6 – Data Types CSCE 343

2 Primitive Types Types not defined in terms of other types
double, int, char, etc. Java signed integer types: byte, short, int, long Decimal types: fixed accuracy (C#, COBOL) Boolean type Advantage readability, reliability C: if (x=5){…} Char type Coding: ASCII (8 bit), Unicode (16 bit)

3 String Types Value: sequence of characters Issues: Typical operations
primitive or char array? Static or dynamic length? Typical operations Assignment, comparison, catenation, substring, pattern matching C, C++: char arrays, (string class) SNOBOL4: primitive Java: String class Built in operations (.equals(), + , substring())

4 String Length Static: set when the string is created
COBOL, Java, C++ string, Python, Ruby Limited Dynamic: varying length up to fixed size char arrays in C using \0. Dynamic: varying length no maxium Perl, JavaScript, Java (StringBuilder)

5 Ordinal Types Type with limited range associated with positive integers: Java: integer, char, boolean Enumeration types Named constants for all values C#: enum days {mon, tue, wed, thu, fri, sat, sun}; enum days x = mon; Aids readability Aids reliability: Compiler checks operations, range

6 Subrange Types A sub-range of an ordinal type Aid to readability
subtype Index is Integer range 0..99 Aid to readability Clear about valid values Reliability Range checks

7 Array Types Array: aggregate of homogenous data elements Issues:
Each element is identified by position Issues: Legal types for subscripts? Range checking? Binding of subscript to ranges. When does array allocation take place? Allow multidimensional and/or ragged arrays Max number of subscripts? Initialization? Slicing?

8 Indexes Types: Range checking: Index Type
FORTRAN, C, C++, Java: integer types only. Pascal: any ordinal type (integer, boolean, char, enumeration) Range checking: C, C++, Perl, FORTRAN: No Java, C#: Yes Perl: Kind of (no error message, returns undef) Index Type static binding

9 Array Categories Must determine binding of subrange
binding memory allocation stack or heap memory Static: a[10] Allocation, subscript range bindings at compile time. (efficient) Fixed stack-dynamic: a[10] Subscript ranges are static, allocation is done at declaration execution (space efficient) Stack dynamic: a[x] Subscript range and storage is done at run-time (declaration) fixed after allocation Fixed heap-dynamic:a[x] Similar to stack dynamic, fixed after allocation (heap) Heap dynamic Everything (range, storage) is dynamic (heap) ArrayList, vector

10 Examples Static: C,C++ static modifier
Fixed stack-dynamic: C, C++ local arrays Stack dynamic: C++ Fixed heap-dynamic: Java Heap-dynamic: Perl, JavaScript foo(){ int a[10]; int b[x]; int c[] = new int[x]; ArrayList d = new ArrayList();

11 Initialization C, C++, Java, C#: Character strings in C, C++:
int list[] = {4,5,7,83}; Character strings in C, C++: char name[] = “Nick”; Arrays of strings in C, C++: char *names[] = {“Bob”, “Jake”, “Jane”}; names[0] is a pointer to ‘B’ (literal char array) Java initialization of String objects: String[] names = {“Bob”, “Jake”, “Joe”}; Java 2D arrays: int[][] a = {{1,2}, {3,4,5}};

12 Array Implementation Contiguous memory Calculating address:
addr(a[i]) = addr(a[0]) + ( i * size ) In general: addr(a[i]) = addr(a[lb]) + ( (i – lb) * size )

13 2D Array Implementation
Contiguous memory: Row-major order (most common) Column-major order (FORTRAN) Address functions: Row major: addr(a[i][j]) = addr(a[0][0]) + (((i * n) + j ) * size)

14 Array Slicing Extracting parts of arrays: Perl and Python
Python example a1 = [0, 1, 2, 3, 4, 5, 6] a2 = a1[3:5] a3 = a1[:4] a4 = a1[3:]

15 In-class Find the memory address of char a[20][15]; float b[15][20];
Make the following assumptions a is allocated memory starting at byte 800 in memory b is allocated memory starting at byte 1800 in memory a float is 4 bytes two dimensional arrays use row-major order Find the memory address of a[10][5] b[5][10]

16 Associative Arrays Like the STL map (key, data value) pairs
Key is index to data value typical data is stored in an array array component selected with hash(key) Python example

17 Record Types Record: aggregate of data elements
Elements (fields) identified by name Before records parallel arrays were used C: struct, Ada: record, COBOL: records Access to elements with dot (.): x.name Operations: Assignment allowed? Comparison?

18 Implementation of Record Types
Compile time descriptor. Runtime descriptor not needed, since this information is available at compile time.

19 Union Types Allows different types at different times
C,C++, no type checking (free union) Ada includes type checking with discriminant. Evaluation: Increase flexibility Potentially unsafe (type checking) Java, C# do not support unions. Example code for struct and union

20 Pointer and Reference Types
Pointer: value is memory address or NULL Indirect addressing Manage dynamic memory (heap) Design issues: What are scope & lifetime of pointer variables? What is the lifetime of heap-dynamic variable? Are there restrictions as to what a pointer can point to? Are pointers used for dynamic storage management and/or indirect addressing Should the language have pointer types, reference types, or both?

21 Pointer Operations Assignment and dereferencing
int *ptrInt, *array, x=5; //assignment ptrInt = &x; array = malloc(x*sizeof(int)); //dereferencing x = (*ptrInt)+1; a[0] = x;

22 Pointer Problems Dangling Pointers int *p; { int x; p = &x;}
//p points to memory that has been reclaimed *char foo(){char s[20]; return s;} yikes! Lost heap-dynamic variables (memory leak) int *ptrArray, a[30]; ptrArray = malloc(1000* sizeof(int)); ptrArray = a;

23 Pointers in C, C++ Point at any variable of correct type
Used for dynamic storage management and addressing Pointer arithmetic (more interesing?) Explicit dereferencing and address-of operators Domain type need not be fixed (void *) generic pointer cannot be dereferenced

24 C Reference types Special kind of pointer type
Primary use: formal parameters See example code What are advantages and disadvantages of pass-by-reference? Java extends C++ reference variables to replace pointers entirely implicitly dereferenced C# includes both references of Java and pointers of C++

25 Heap Management Two approaches to garbage collection:
Reference counters (eager approach): done incrementally heap cell store count of references to it mark-sweep (lazy approach): done when available cell list is empty heap cell has a “garbage bit” set all garbage bits on check all pointes to turn off garbage bit sweep to recover

26 Exam 1 review guide


Download ppt "Chapter 6 – Data Types CSCE 343."

Similar presentations


Ads by Google