22_3014_033004_Unit 2 Part 1 Elementary Data Types
22_3014_033004_Unit 2 Part 1 Elementary Data Types
● A descriptor contains two or more long words that describe the standard
data type, size, and address of the data specified by the argument
● Different types of descriptors
○ Fixed-Length Descriptor
○ Dynamic String Descriptor
● The Fixed-Length Descriptor applies to scalar data (non-structured data
type) and fixed-length strings.
● The Dynamic-String Descriptor applies to dynamically allocated strings.
Descriptor
● Advantage
○ Accuracy
● Disadvantages:
○ Limited range
○ Wastes memory
Boolean Types
Advantage:
Readability
Character Types
Character String Types
Character String Types
● Character string type is one in which the values consist of sequences of characters
primitive type?
● Limited dynamic length - may need a run-time descriptor for length (but not in C and
C++ because the end of a string is marked with the null character)
● First approach :
○ Using linked list
○ Disadvantage - Extra storage for links and complexity of operations
● Second approach :
○ Store as array of pointers to individual characters allocated in a heap.
○ Disadvantage- Still uses extra memory
Character String Types …Implementation
● Third approach:
○ To store complete strings in adjacent storage cells
○ When a string grows and adjacent storage is not available, a new area of memory
is found that can store the complete new string and the old part is moved to this
area, and the memory cells for the old string are deallocated.
○ This results in faster string operations and requires less storage
○ Disadvantage : = Allocation / deallocation process is slower.
Used Defined Ordinal Types
An ordinal type is one in
which the range of
possible values can be
easily associated with
the set of positive
integers
Enumeration Types
● All possible values, which are named constants, are provided in the definition
● C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
● The enumeration constants are typically implicitly assigned the integer values, 0, 1, …, but
can be explicitly assigned any integer literal in the type’s definition
Design issues
● Is an enumeration constant allowed to appear in more than one type definition, and if so,
how is the type of an occurrence of that constant checked?
● Are enumeration values coerced to integer?
● Any other type coerced to an enumeration type?
Enumeration Types
● In languages that do not have enumeration types, programmers usually simulate them with
integer values.
● E.g. Fortran 77, use 0 to represent blue and 1 to represent red:
● Problem:
○ There is no type checking when they are used.
○ It would be legal to add two together. Or they can be assigned any integer value thus
destroying the relationship with the colors.
Enumeration Types-Design
● In C++, we could have
● In Java, all enumeration types are implicitly subclasses of the predefined class Enum. They
can have instance data fields, constructors and methods.
Enumeration Types-Design
Java Example
Enumeration days;
Vector dayNames = new Vector();
dayNames.add("Monday");
…
dayNames.add("Friday");
days = dayNames.elements();
while (days.hasMoreElements())
System.out.println(days.nextElement());
Enumeration Types-Design
● C# enumeration types are like those of C++ except that they are never coerced to integer.
● Operations are restricted to those that make sense.
● The range of values is restricted to that of the particular enumeration type.
Enumeration Types- Evaluation
● Static: subscript ranges are statically bound and storage allocation is static
(before run‐ time)
● Advantage: efficiency (no dynamic allocation/deallocation required)
● Example: In C and C++ arrays that include the static modifier are static
● static int myarray[3] = {2, 3, 4};
● int static_array[7];
Subscript Bindings and Array Categories fixed stack‐dynamic
● subscript ranges are statically bound, but the allocation is done at declaration time
● Advantage: space efficiency
● Example: arrays without static modifier are fixed stack ‐dynamic
● int array[3] = {2, 3, 4};
● E.g. void foo()
{
int fixed_stack_dynamic_array[7];
/* ... */
}
Subscript Bindings and Array Categories Stack‐dynamic
● Stack‐dynamic: subscript ranges are dynamically bound and the storage
allocation is dynamic (done at run‐ time)
● Advantage: flexibility (the size of an array need not be known until the array is to
be used)
void foo(int n)
● Example: In Ada, you can use stack‐dynamic arrays as {
int stack_dynamic_array[n];
Get(List_Len);
declare List: array (1..List_Len) of Integer /* ... */ }
void foo(int n) {
int * heap_dynamic_array = malloc(n * sizeof(int));
}
Static int static_array[7];
void foo()
fixed stack‐dynamic
{ int fixed_stack_dynamic_array[7]; }
void foo(int n)
Stack‐dynamic
{ int stack_dynamic_array[n];}
void foo(int n)
heap_dynamic_array
{ int * heap_dynamic_array = malloc(n * sizeof(int)); }
Subscript Bindings and Array Categories
Array Initialization
Some language allow initialization at the time of storage allocation
● Fortran
List (3) Data List /0, 5, 5/ // List is initialized to the values
● C, C++, Java, C# example
int list [] = {4, 5, 7, 83}
● Character strings in C and C++
char name [] = “freddie”; // eight elements, including last element as null
character
● Arrays of strings in C and C++
char *names [] = {“Bob”, “Jake”, “Joe”];
Array Initialization
● Some language allow initialization at the time of storage allocation
● Fortran Declaration:
○ Vector(3:6) Four element from third to sixth
○ Mat(1:3,2) Second Column of Mat
○ Mat(3, 1:3) Third row of Mat
○ Vector(2:10:2) Complex size , second, fourth ,sixth, eight, tenth
○ Vector(/3,2,1,8/)
Slice
Implementation of array Types
Implementation of array Types
Address Calculation in One Dimensional Array:
Address of A [ I ] = B + W * ( I – LB )
Where,
B = Base address
W = Storage Size of one element stored in the array (in byte)
I = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Implementation of array Types
Address of A [ I ] = B + W * ( I – LB )
● I=3 , B=1100 W=4 LB=0
● A[3]= 1100+4*(3-0)
● A[3]= 1100+12 =1112
Implementation of array Types
Address Calculation in Double (Two) Dimensional Array:
Implementation of array Types
Implementation of array Types
Address Calculation in Double (Two) Dimensional Array:
● B = Base address
● I = Row subscript of element whose address is to be found
● J = Column subscript of element whose address is to be found
● W = Storage Size of one element stored in the array (in byte)
● Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
● Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
● M = Number of row of the given matrix
● N = Number of column of the given matrix
Implementation of array Types
Address Calculation in Double (Two) Dimensional Array:
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
Design Issues
Design Issues
$lookup{"dave"} = 7634;
Associative Array
01 EMP-REC.
02 EMP-NAME.
FIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the
employee’s first name
References to Records
struct { fully qualified reference: lists all
struct { person.name.first
} name;
Operations on Records
Operations on Records
Implementation of Record Type
The
assignment
operation
j = *ptr
Dangling Pointer
Dangling pointers (dangerous)
A pointer pointing to data that does not exist anymore is called a
dangling pointer.
d is a dangling pointer.
Dangling Pointer
#include <stdio.h>
int main()
{
int *ptr=(int *)malloc(sizeof(int));
int a=560;
ptr=&a;
free(ptr);
return 0;
}
1st Solution to Dangling Pointer
If we assign the NULL value to the 'ptr',
then 'ptr' will not point to the deleted
memory. Therefore, we can say that ptr
is not a dangling pointer, as shown in the
below image:
Lost Heap dynamic Variable
● An allocated heap-dynamic variable that is no longer
accessible to the user program (often called garbage)
● Pointer p1 is set to point to a newly created heap-dynamic
variable
● Pointer p1 is later set to point to another newly created
heap-dynamic variable
● The process of losing heap-dynamic variables is called
memory leakage
Lost Heap dynamic Variable
Pointer Arithmetic in C and C++
float stuff[100];
float *p;
p = stuff;
Use references
Use pointers:
For example for arrays (Note that array access is implemented using pointer arithmetic).
● To implement data structures like linked list, tree, etc and their algorithms because to
point different cell, we have to use the concept of pointers.
Implementation of Pointers and Reference Types
Representations of Pointers
111333
112233
pointer2
delete pointer1;
111222 112233 445566
112233 NULL
5
pointer Tombstone Dynamic heap variable
● When memory is allocated, space for one more cell called the lock cell is
allocated. This cell is assigned a value.
● The pointer which points to this memory is stored as a tuple containing an
address and a key.
● A pointer is allowed to access the memory only if the values of the lock and
keys match.
● Otherwise, it will throw a runtime error.
Dangling Pointer solution using
Locks-and-keys:
• Int * x = new int (10);
• Int *y=x;
• delete y;
Key address Lock allocated memory
x 5 0x12ff66 5 10
y 5 0x12ff66
delete y;
x 5 0x12ff66 67 10
If they match, the access is legal; otherwise the access is
treated as a run-time error. This approach is save as
y 5 0x12ff66 it doesn’t access other program data, as the heap
dynamic variable may be assigned to another program.
Dangling Pointer solution using
Locks-and-keys:
count
char * p1 = new char (111); P1 2 111
char * p2=p1; Dynamic heap variable
P2
count
P1 1 222
P2 1 111
count
P1 1 222
count
count
char * p1 = new char (111); P1 2 111
char * p2=p1; Dynamic heap variable
P2
count
P1 0 111
Dynamic heap variable
delete p1; P2
If the reference counter reaches zero, it means that no program pointers are pointing at the cell, and it
has thus become garbage and can be returned to the list of available space.
1-Reference Counter
// STEP 1
Object a = new Integer (100);
Object b = new Integer (99);
// Step2
a=b;
Main()
{
…
buidDog();
….
}
Reference
counting does not
detect garbage One of the solution
with cyclic is to use mark and
references. sweep alg.
1-Reference Counter – problem
reclaim garbage
Mark-sweep
Reference counters (lazy approach)
(eager approach): reclamation occurs
reclamation is when the list of
gradual available space
becomes empty
Mark-Sweep
● The run-time system allocates storage cells as requested and disconnects pointers
from cells as necessary; mark-sweep then begins
○ Every heap cell has an extra bit used by collection algorithm