Download Basic Structured Data Types

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Array data structure wikipedia , lookup

Transcript
CmSc315 Programming Languages
Chapter 5: Types, Part III
C. Structured Data Types
1. Mechanisms to create new data types.
Structured data
Homogeneous: arrays, lists, sets,
Non-homogeneous: records
Subprograms
Type declarations – to define new types and operations (Abstract data types)
Inheritance
A data structure is a data object that contains other data objects as its
elements or components.
1.1. Specifications
a. Data specifications

Number of components
Fixed size - Arrays
Variable size – stacks, lists. Pointer is used to link components.

Type of each component
Homogeneous – all components are the same type
Heterogeneous – components are of different types

Selection mechanism to identify components – index, pointer
Two-step process:
referencing the structure
selection of a particular component


Maximum number of components
Organization of the components:
 simple linear sequence
 multidimensional structures:
 separate types (Fortran)
 vector of vectors (C++)
1
b. Operations on data structures



Component selection operations
Sequential (as in lists)
Random (as in arrays)
Insertion/deletion of components
Whole-data structure operations
Creation/destruction of data structures
1.2. Implementation of data structure types

Storage representation
Includes:
a. storage for the components
b. optional descriptor - to contain some or all of the attributes
-
Sequential representation: the data structure is stored in a single
contiguous block of storage that includes both descriptor and
components. Used for fixed-size structures, homogeneous structures
(arrays, character strings)
-
Linked representation: the data structure is stored in several
noncontiguous blocks of storage, linked together through pointers.
Used for variable-size structured (trees, lists)
Stacks, queues, lists can be represented in either way. Linked
representation is more flexible and ensures true variable size, however it
has to be software simulated.

Implementation of operations on data structures
Component selection in sequential representation
Base address plus offset calculation. Add component size to
current location to move to next component.
Component selection in linked representation
Move from address location to address location following the
chain of pointers.

Storage management
Access paths to a structured data object - to endure access to the object for
its processing. Created using a name or a pointer.
2
Two central problems:
Garbage – data object is bound but access path is destroyed.
Memory cannot be unbound.
Dangling references: the data object is destroyed, but the access
path still exists.
2. Arrays
Array: indexed sequence of values
Implementation of array operations:
a. Access - can be implemented efficiently if the length of the components of the
array is known at compilation time. The address of each selected element can
be computed using an arithmetic expression.
b. Whole array operations, e.g. copying an array - may require much memory.
Equivalence between pointers and arrays: - see the example from Wednesday
Two dimensional arrays : “row-major and “column-major” representation
How to compute the address of an element:
Exercise:
Let A be an array declared as mytype A[row][col]; Let B be the base address, assigned by the
compiler, and L be the size of each component.
a. Give the formula to compute the relative address of A[j][k] in a "row-major"
representation, given the lower bound of j and k to be 0.
b. Give the formula to compute the relative address of A[j][k] in a "column-major"
representation, given the lower bound of j and k to be 0.
3
3. Strings
Implemented as arrays.
Terminating symbol: null (‘\0’)
In Java, Perl, Python, a string variable can hold an unbounded number of characters.
Libraries of string operations and functions.
4. Records (Structures)
A record is data structure composed of a fixed number of components of
different types. The components may be heterogeneous, and they are named with
symbolic names.
Specification of attributes of a record:
Number of components
Data type of each component
The selector used to name each component.
Implementation
Storage: single sequential block of memory where the components are
stored sequentially.
Selection: provided the type of each component is known, the location can
be computed at translation time.
Referencing operation: selects a particular component of the record
Example: in C++ referencing is implemented by means of the ‘dot’ operator
struct employeeType
{
int id;
char name[25];
int age;
float salary;
char dept;
};
struct employeeType employee;
...
employee.age = 45;
Note on efficiency of storage representation
For some data types storage must begin on specific memory
boundaries (required by the hardware organization). For example, integers must be
4
allocated at word boundaries (e.g. addresses that are multiples of 4). When the structure
of a record is designed, this fact has to be taken into consideration. Otherwise the actual
memory needed might be more than the sum of the length of each component in the
record. Here is an example:
struct employee
{ char Division;
int IdNumber; };
The first variable occupies one byte only. The next three bytes will remain unused and
then the second variable will be allocated to a word boundary.
Careless design may result in doubling the memory requirements.




Used first in Cobol, PL/I
Absent from Fortran, Algol 60
Common to Pascal-like, C-like languages
Omitted from Java as redundant
5. Other structured data objects
Records and arrays with structured components: a record may have a
component that is an array, an array may be built out of components that are records.
Lists and sets - lists are usually considered to represent an ordered sequence of
elements, sets - to represent unordered collection of elements.
Executable data objects
5