Bài giảng Chapter 6 data types

102 3 0
Bài giảng Chapter 6   data types

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter Data Types ISBN 0-321-33025-0 Chapter Topics • Introduction • Primitive Data Types • Character String Types • User-Defined Ordinal Types • Array Types • Associative Arrays • Record Types • Union Types • Pointer and Reference Types Copyright â 2006 Addison-Wesley All rights reserved 1-2 Introduction ã A data type defines a collection of data values and a set of predefined operations on those objects • Some data types are specified by type constructors which are used to form type expressions („[]‟, „*‟, „()‟ in C for array, pointer, function, respectively) • A descriptor is the collection of the attributes of a variable It is used for type checking and by allocation and deallocation operations • Design issues: What operations are provided for variables of the type and how are they specified? Copyright © 2006 Addison-Wesley All rights reserved 1-3 Primitive Data Types • Almost all programming languages provide a set of primitive data types – Primitive data types: Those not defined in terms of other data types • Some primitive data types are merely reflections of the hardware ã Others require little software support Copyright â 2006 Addison-Wesley All rights reserved 1-4 Primitive Data Types: Integer • Almost always an exact reflection of the hardware so the mapping is trivial – Java: byte, short, int, long – Ada: SHORT INTEGER, INTEGER and LONG INTEGER • The leftmost bit is set to indicate negative and the remainder of the bit string represents the absolute value of the number • Most computers now use a notation called two‟s complement to store negative integers, which is convenient for addition and subtraction Copyright © 2006 Addison-Wesley All rights reserved 1-5 Primitive Data Types: Floating Point • Model real numbers, but only as approximations for most real values • Languages for scientific use support at least two floating-point types (e.g., float and double; sometimes more) • The representation of this type is not very much like the hardware Copyright © 2006 Addison-Wesley All rights reserved 1-6 IEEE Floating Point Formats Copyright © 2006 Addison-Wesley All rights reserved 1-7 Primitive Data Types: Decimal • For business applications • Store a fixed number of decimal digits • The representations of data types are called Binary Coded Decimal (BCD) – Two digits per byte • Advantage – Accuracy • Disadvantages – The range of values is restricted because no exponents are allowed – BDC takes more storage then binary representation Copyright © 2006 Addison-Wesley All rights reserved 1-8 Primitive Data Types: Boolean • Simplest of all • Range of values: two elements, one for “true” and one for “false” • Could be implemented as bits, but often as bytes – Advantage: readability Copyright © 2006 Addison-Wesley All rights reserved 1-9 Primitive Data Types: Character • Stored as numeric codings • Most commonly used coding: ASCII • An alternative, 16-bit coding: Unicode – Includes characters from most natural languages – Originally used in Java – C# and JavaScript also support Unicode Copyright © 2006 Addison-Wesley All rights reserved 1-10 Example real, target :: a, b real, pointer :: pa pa => a … b = pa * c … pa = 1.2345 print *, 'a = ', a Copyright © 2006 Addison-Wesley All rights reserved INTEGER, DIMENSION(:), ALLOCATABLE :: a !rank … ALLOCATE(a(100)) … DEALLOCATE(a) 1-88 Reference Types • C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters – A C++ reference type is a pointer that is always implicitly dereferenced  It must be initialized with the address of some variable in its definition  After initialization can never be set to reference any other variable  The implicit dereference prevents assignment to the address of a reference variable Copyright © 2006 Addison-Wesley All rights reserved 1-89 Reference Types (cont.) • Java extends C++‟s reference variables and allows them to replace pointers entirely – No pointer arithmetic – Can only point at objects (which are all on the heap) – No explicit deallocator (garbage collection is used), that means there can be no dangling references – Dereferencing is always implicit • C# includes both the references of Java and the pointers of C++ Copyright © 2006 Addison-Wesley All rights reserved 1-90 Implementation of Pointer and Reference Types • Representation of pointers and references – Large computers use single values – Intel microprocessors use segment and offset • Dangling pointer problem Tombstone: extra heap cell that is a pointer to the heap-dynamic variable  The actual pointer variable points only at tombstones  When heap-dynamic variable deallocated, tombstone remains but set to nil Copyright © 2006 Addison-Wesley All rights reserved 1-91 Implementation of Pointer and Reference Types (cont.) Copyright © 2006 Addison-Wesley All rights reserved 1-92 Implementation of Pointer and Reference Types (cont.) Locks and keys: Pointer values are represented as pairs  Heap-dynamic variables are represented as variable plus cell for integer lock value  When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer  When a heap-dynamic variable is deallocated explicitly, its lock value is cleared Then, although a pointer has the same address value but its key value will no longer match the lock, so the access will not be allowed Copyright © 2006 Addison-Wesley All rights reserved 1-93 Evaluation of Pointers and References • Dangling pointers and dangling objects are problems • Pointers are like goto's - they widen the range of cells that can be accessed by a variable • Pointers or references are necessary for dynamic data structures - so we can't design a language without them Copyright © 2006 Addison-Wesley All rights reserved 1-94 Garbage Collection • Problems with garbage collection – Many languages leave it up to the programmer to design without garbage creation - this is very hard – Others arrange for automatic garbage collection • Garbage collection with reference counts – Does not work for circular structures – Works great for strings – Should also work to collect unneeded tombstones Copyright © 2006 Addison-Wesley All rights reserved 1-95 Copyright © 2006 Addison-Wesley All rights reserved 1-96 Garbage Collection (cont.) • Mark-and-sweep – Achieved successfully in Ada, Java, ML, … – It processes in three main steps The collector walks through the heap, tentatively marking every block as “useless” Beginning with all pointers outside the heap, the collector recursively explores all linked data structures in the program, marking each newly discovered block as “useful” The collector again walks through the heap, moving every block that is still marked “useless” to the free list Copyright © 2006 Addison-Wesley All rights reserved 1-97 Garbage Collection (cont.) – Several potential problems:  The collector be able to tell where every “in-use” block begins and ends  In a language with variable-size heap blocks, every block must begin with an indication of its size  The collector must be able in Step to find the pointers contained within each block Copyright © 2006 Addison-Wesley All rights reserved 1-98 Garbage Collection (cont.) • Garbage collection with pointer reversal – The exploration step (Step 2) of mark-and-sweep collection is naturally recursive  The implementation needs a stack whose maximum depth is proportional to the longest chain through the heap  In practice, the space for this stack may not be available: after all, we run garbage collection when we‟re about to run out of space! – As the collector explores the path to a given block, it reverses the pointers it follows, so that each points back to the previous block Copyright © 2006 Addison-Wesley All rights reserved 1-99 Copyright © 2006 Addison-Wesley All rights reserved 1-100 Garbage Collection (cont.) • Stop-and-Copy – System divide the heap into two regions of equal size All allocation happens in the first half When this half is (nearly) full, the collector begins its exploration – Each reachable block is copied into the second half of the heap Any other pointer that refers to the same block is set to point to the new location – When the collector finishes its exploration, all useful objects have been moved (and compacted) into the second half of the heap, and nothing in the first half is needed anymore Copyright © 2006 Addison-Wesley All rights reserved 1-101 Summary • The data types of a language are a large part of what determines that language‟s style and usefulness • The primitive data types of most imperative languages include numeric, character, and Boolean types • The user-defined enumeration and subrange types are convenient and add to the readability and reliability of programs • Arrays and records are included in most languages • Pointers are used for addressing flexibility and to control dynamic storage management Copyright © 2006 Addison-Wesley All rights reserved 1-102 .. .Chapter Topics • Introduction • Primitive Data Types • Character String Types • User-Defined Ordinal Types • Array Types • Associative Arrays • Record Types • Union Types • Pointer... specified? Copyright © 20 06 Addison-Wesley All rights reserved 1-3 Primitive Data Types • Almost all programming languages provide a set of primitive data types – Primitive data types: Those not defined... data types • Some primitive data types are merely reflections of the hardware • Others require little software support Copyright © 20 06 Addison-Wesley All rights reserved 1-4 Primitive Data Types:

Ngày đăng: 23/03/2022, 08:29

Tài liệu cùng người dùng

Tài liệu liên quan