You are on page 1of 33

TOPICS

Definition . Features . Hierarchical structure. Data structures. Storage management.

DEFINITION OF PROGRAMMING LANGUAGE

A notation with which people can communicate algorithms to computer and to one another. There are hundreds of programming languages. Eg, FORTRAN, COBOL, SNOBOL, C, C++, JAVA etc..

They differ in their degree of closeness to natural or mathematical language. They differ in their degree of closeness to machine language. They differ in the type of problem for which they are best suited.

FEATURES OF PROGRAMMING LANGUAGES

1. ease of understanding: easy to read and write. 2. naturalness: ease with which one can express an algorithm in that language. Like FORTRAN for numerical & mathematical computations. COBOL for business applications. SNOBOL for string manipulations.

3. portability: run programs on a variety of machines. 4. efficiency of use: this covers a number of aspects of both program & language design. Implement programs in a way that makes efficient use of time. The design of language affects the computation.

HIERARCHICAL STRUCTURE OF PROGRAMMING LANGUAGES

A programming language is a notation for specifying a sequence of operations to be carried out on data objects. Both data objects and operations can be grouped into a hierarchy.

PROGRAM

SUBROUTINES & BLOCKS

STATEMENTS

EXPRESSIONS

DATA REFERENCES

OPERATORS

FUNCTIONS

#include<iostream.h> #include<conio.h>

int add();
void main() { int d;

d=add();
cout<<addition is:<<d; getch(); }

FUNCTION CALL

BLOCK

int add()
{ int a,b,c; cout<<enter 2 values:;

STATEMENT

SUBROUTINE
EXPRESSION OPERATOR

cin>>a>>b;
c=a+b; return c; }

DATA STRUCTURES

Data structure is a set of primitive data elements and other data structures, together with a set of structural relations among its components. A data structure is defined recursively. Data structure is a collection of data elements. Each data element has 3 parts namely; its name, its value & its relative position in the structure. int a[5]=10;

Some functions associated with data structures are: 1. constructors: creates instances of data structures. 2. destructors: dismantle data structures to make their storage area available again. 3. selectors: access data structures.

TYPES OF DATA STRUCTURES

1. 2. 3. 4. 5.

arrays. character strings. Record structure. list structures. stacks.

ARRAYS

An array is a collection of elements of some fixed type, laid out in a k dimensional rectangular structure. Index is the measure of distance along each dimensions.
FIXED SIZE ADJUSTABLE

ONE DIMENSIONAL

MULTI DIMENSIONAL

1.

2.
3. 4. 5.

1. fixed size one dimensional arrays: if the size of the array is known at compile time, then it is expedient to implement the array as a block of consecutive words in memory. A data descriptor for one dimensional array contains: Data type. Element type (integer, character etc). Number of memory units per block. Lower limit. Upper limit.

2. fixed size multidimensional array: a 2 dimensional array is stored in one of the 2 forms namely row major or column major. PL/I used row major, FORTRAN uses column major.
A[1,1] A[1,2] A[1,3]

A[1,1]

A[1,1]

A[1,2]
A[2,1] A[2,2] A[2,3]

A[2,1]
A[1,2] A[2,2] A[1,3] A[2,3] COLUMN MAJOR

A[1,3] A[2,1] A[2,2] A[2,3] ROW MAJOR

3. adjustable arrays: size of arrays to be specified dynamically that is, run time. Eg, ALGOL, PL/I. APL allows even the number of dimensions to vary at run time. So we need dynamic storage allocation scheme.

CHARACTER STRINGS

They are one dimensional arrays whose elements are characters. They may thus be represented as arrays.

RECORD STRUCTURE

It is a tree, with the fields of the record being the children of the root, the subfields being children of these, & so on. The paradigm of record structure is a mailing list. They are implemented as a block of memory. We must determine the width and offset for each field to determine how much storage is necessary. The data descriptor must contain offset, width and data type for each field

Example: we want to store names and addresses of a collection of people. A PL/I structure would look like:
NAME width= 36.
ADDRESS width= 64. 3 TITLE CHARACTER(6), 3 FIRST CHARACTER(15), 3 LAST CHARACTER(15), 2 ADDRESS, 3 STREET CHARACTER(30), FRIEND width = 100. Bytes of storage = 100000. Offset of ADDRESS from FRIEND = 36. Offset of LAST = 21. 2 NAME,

1 FRIENDS(1000),

3 TOWN CHARACTER(15),
3 STATE CHARACTER(15), 3 ZIP FIXED DECIMAL(5);

LIST STRUCTURE

List structures are formed from elements which are records with 2 fields, called CAR and CDR. CAR- contents of address register. CDR- contents of decrement register. Each of these fields contains either an atom(primitive data type), the null pointer, or the address of another record. LISP is an example of a language that allows list structures.

CAR

CDR

A B C

. . .

ARRAY REPRESENTATION OF LISP LIST STRUCTURE

STACKS

A stack is a linear list that we operate on only at the beginning or top end. 2 operations on stack: push & pop. 2 ways that stack appear in programming languages: First, like PL/I provides to define stack as a data type using CONTROLLED attribute and ALLOCATE & FREE operations. Second, use a block of storage in which an array of the maximum number of elements can be put. Use a pointer to one of these elements to indicate the current top element. Push & pop by moving pointer.

STORAGE MANAGEMENT

There are a number of elements to which storage must be allocated in order to execute the object program. Most obvious is storage required for object program, data structures, variables & constants. Less obvious is storage required for procedure linkage information, parameter transmission, buffers etc.

STORAGE ALLOCATION

STATIC STORAGE ALLOCATION

DYNAMIC STORAGE ALLOCATION

STACK ALLOCATION

HEAP ALLOCATION

STATIC STORAGE ALLOCATION

If the size of every data item can be determined by the compiler and if recursive procedure calls are not permitted, then the space for all programs and data can be allocated at compile time, i.e. statistically. It is easy to implement and requires no run time support for allocating storage. This method is used in FORTRAN.

DYNAMIC STORAGE ALLOCATION

If a programming language permits either recursive procedures or data structures whose size is adjustable, then some sort of dynamic storage management is necessary.

STACK ALLOCATION

A region of consecutive words of memory will be used as a stack, in the sense that allocated storage will always be added to the top by incrementing a stack pointer. It operates as LIFO(last in first out) mode. It cannot be used to handle arbitrary allocations and releases of storage. Used to handle the basic storage requirements of a block structured language. Stack allocation is useful for handling recursive procedures.

We can collect all the fixed size storage required by variables declared in one procedure into a single chunk of storage called an activation record. Activation record is a block of memory normally used for procedures. The activation record might contain following items: 1. storage for simple names, and pointers to arrays and other data structures local to the procedure. 2. temporaries for expression evaluation and parameter passing. 3. information regarding attributes for local names and formal parameters, when these cannot be determined at compile time. 4. The return address. 5. a pointer to the activation record of the caller.

ARRAY

ARRAY

fixed size data & pointer to variable sized data

return address

Stack pointer
pointer to next record

ACTIVATION RECORD

EXAMPLE:
a b Block C c d Block D e
C D B A B A B A A B A A B A

Block A

Block B

f g BLOCK STRUCTURE PROGRAM

BEHAVIOR OF STACK

HEAP ALLOCATION

Heap allocation is useful for implementing data whose size varies as the program is running. Example, strings in SNOBOL. It involves taking a large block of memory and dividing it into variable length blocks, some used for data and some free. When a piece of data is created we must find a free block of sufficient size. When data is no longer needed, its block becomes free. The portions of the heap not currently in use are linked together in an available space list.

Available space

free

value of Y

free

value of X

free

value of Z

free

X Y Z

. . .

HEAP ALLOCATION

You might also like