Вы находитесь на странице: 1из 9

HOMEWORK NO:2

CAP204: Fundamentals of Data Structures

DOA: 23/09/09 DOS: 07/10/09

Part-A

1. How debugging pointers are beneficial in data structures?

Ans. Abstract debugging (a term coined by Francois Bourdoncle) stands for the
inference of necessary conditions for the correctness of programs wrt. invariant or
intermittent assertions (initial values not satisfying the conditions are bugs). In
the system considered by Bourdoncle, assertions could be used to described
interval ranges of program variables; the necessary conditions were inferred using
static analysis based on abstract interpretation techniques. In a first step

towards extending this system, we analyze that abstract debugging is abstract


model checking.

That is, a conservative approximation of the initial states satisfying certain


temporal

logic properties is computed. We then ask how we can extend the framework to
programs

over pointers; i.e., how can we formulate assertions over “intervals” of pointers,
and what are

the corresponding abstract interpretation techniques. We restrict this question to


tree-like

data structures as they were considered in the first shape-analysis papers by Neil
Jones and

others. We establish a connection between that work on shape-analysis and


model checking

of pushdown systems. An extension of pushdown systems is used as an


abstraction of while

programs over trees. In summary, we derive that abstract debugging of while


programs over

trees is model checking of extended pushdown systems.


2. How memory is allocated and deallocated dynamically? Illustrate various functions
for it?

Ans. 17. A computer readable memory recording a circuit synthesizing program for a circuit
synthesizing of a semiconductor circuit for executing a program with a function of pointers and
dynamic allocation, said circuit synthesizing programcomprising the steps of: resolving pointer and
dynamic allocation in a code of the program; and changing the code of the program into another code
which does not contain said pointer and said dynamic allocation; wherein the semiconductor
circuitexecutes the program with a function of pointers and dynamic allocation when a synthesis of said
semiconductor circuit is performed.

18. The computer readable memory recording a circuit synthesizing program for a circuit synthesizing
of as set forth in claim 17, wherein said program is C-language program.

19. The computer readable memory recording a circuit synthesizing program for a circuit synthesizing
of as set forth in claim 17, said circuit synthesizing program further comprising: changing said code
changed by said resolution step into acode of Hardware Description Languages, and synthesizing said
semiconductor circuit based on said code of Hardware Description Languages.

20. The computer readable memory recording a circuit synthesizing program for a circuit synthesizing
of as set forth in claim 17, said resolution step of said circuit synthesizing program including checking
the kind of the variable which is theobject of dynamic allocation and the quantity of a memory assigned
in the code, performing beforehand variable declaration of the variable which is the object of the
dynamic allocation statically in the converted code, and replacing a command whichperforms dynamic
allocation in the code with a command which gives the pointer to the variable by which variable
declaration was carried out.

21. A computer readable memory recording a circuit synthesizing program for a circuit synthesizing of
a semiconductor circuit for executing a program with a function of pointers and dynamic allocation,
said circuit synthesizing programcomprising the steps of: resolving pointer and dynamic allocation in a
code of the program; and changing the code of the program into another code which does not contain
said pointer and said dynamic allocation; and said resolution step includingchecking the kind of the
variable which is the object of dynamic allocation and the quantity of a memory assigned in the code,
performing beforehand variable declaration of the variable which is the object of the dynamic
allocation statically in theconverted code, replacing a command which performs dynamic allocation in
the code with a command which gives the pointer to the variable by which variable declaration was
carried out, wherein the semiconductor circuit executes the program with a functionof pointers and
dynamic allocation when a synthesis of said semiconductor circuit is performed.

22. A computer readable memory recording a circuit synthesizing program for a circuit synthesizing of
a semiconductor circuit for executing a program with a function of pointers and dynamic allocation,
said circuit synthesizing programcomprising the steps of: resolving pointer and dynamic allocation in a
code of the program; and changing the code of the program into another code which does not contain
said pointer and said dynamic allocation; and said resolution step of said circuitsynthesizing program
including a pointer analysis step of finding a pointer variable in the code, and checking information on
the a variable which substitutes an address to each pointer variable, executing variable declaration of a
structure objectcomprising a variable tag and a integer variable index in the code after the conversion
corresponding to each pointer variable, which variable tag shows the kind of variable substituted to a
pointer variable, and which integer variable index records theaddition--and--subtraction processing in
the code to the pointer variable, replacing a command which substitutes the address of other variables
to said pointer variable in said code with a command which substitutes information on the kind of
othervariables to the variable tag and substitutes value "0" to the variable index, replacing a command
which fluctuates the value of the pointer variable in the code with a command which fluctuates the
value of the variable index of the structure objectcorresponding to the pointer variable, replacing a
command which refers to the address shown by the pointer variable with a command which refers to
the value of the arrangement position of the value of the variable index in the variable shown by
thevariable tag, checking the kind of the variable which is the object of dynamic allocation and the
quantity of a memory assigned in the code, performing beforehand variable declaration of the variable
which is the object of the dynamic allocationstatically in the converted code, replacing a command
which performs dynamic allocation in the code with a command which gives the pointer to the variable
by which variable declaration was carried out, wherein the semiconductor circuit executes theprogram
with a function of pointers and dynamic allocation when a synthesis of said semiconductor circuit is
performed.

23. The computer readable memory recording a circuit synthesizing program for a circuit synthesizing
of as set forth in claim 22, said circuit synthesizing program further comprising: recording the number
of block allocated to block of thepointer variable, and accessing the block by specifying the number of
block in free area by an allocator, wherein synthesizing the semiconductor circuit executes
management of optimized free area.

24. The computer readable memory recording a circuit synthesizing program for a circuit synthesizing
of as set forth in claim 22, said resolution step including when a size of the variable to be allocated is
constant, and dynamically-allocatedvariable is both allocated and deallocated within the same
unbounded loop, performing beforehand statically variable declaration of the array variable which is
the same size as the dynamically-allocated variable in the converted code, replacing a commandwhich
performs dynamic allocation with the command which references the variable to the array variable by
which variable declaration was carried out, and removing the command performs dynamic deallocation
in the code.

Functions and methods>>

The pointer analyses and memory representation presented in "Memory representation" support the
complete ANSI C syntax. In this description of the preferred embodiment however, this inventors
define synthesizable subset. This subset includesmalloc/free as well as all types of pointers and type
casting. Nevertheless this inventors set the following two restrictions.

The first restriction applies to systems described as a set of parallel processes: pointers that
reference data outside of the scope of a process (e.g. global variables or data internal to some other
process) are not allowed. Their resolutionwould require the synthesis of some kind of interface
between the processes. Such interface is usually defined during system partitioning and, hence,
before synthesis. As a result, memory allocated in one process is assumed to be accessed
anddeallocated only within this same process.

The second limitation stems from the fact that most commercial synthesis tools also have
restrictions on functions. Recursions are usually not supported. Procedures that are mapped to
components typically have restrictions both on theirfunctionality and their parameters. For
example, the same function called within different contexts may usually not be shared. Besides,
most synthesis tools do not synthesize parameter passed by reference, because this is not supported
by most HDLsyntax. The synthesis of functions in C, and therefore the resolution of pointers and
malloc/free inside of functions, is beyond the scope of this description of the preferred embodiment.

Other restrictions are also added in the implementation section in order to be able to translate C
models into Verilog synthesizable by commercial high-level synthesis tools. These restrictions are
however not required for the resolution ofpointers and dynamic memory allocation and do not
apply for the next "SYNTHESIZING MALLOC AND FREE".

Synthesizing Malloc and Free

3. Pointers play important role in implementation of various data structures .give your
views in this regard

Ans. Pointers plays an important role in implementation of various data structures.

A data structure is a group of data elements grouped together under one name. These data
elements, known as members, can have different types and different lengths. Data structures are
declared in C++ using the following syntax:

struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;

where structure_name is a name for the structure type, object_name can be a set of valid
identifiers for objects that have the type of this structure. Within braces { } there is a list with the
data members, each one is specified with a type and a valid identifier as its name.

The first thing we have to know is that a data structure creates a new type: Once a data structure
is declared, a new type with the identifier specified as structure_name is created and can be used
in the rest of the program as if it was any other type. For example:

1struct product {
2
3 int weight;
4
5
6
7 float price;

} ;

product apple;

product banana,
melon;

We have first declared a structure type called product with two members: weight and price,
each of a different fundamental type. We have then used this name of the structure type
(product) to declare three objects of that type: apple, banana and melon as we would have done
with any fundamental data type.

Once declared, product has become a new valid type name like the fundamental ones int, char
or short and from that point on we are able to declare objects (variables) of this compound new
type, like we have done with apple, banana and melon.

Right at the end of the struct declaration, and before the ending semicolon, we can use the
optional field object_name to directly declare objects of the structure type. For example, we can
also declare the structure objects apple, banana and melon at the moment we define the data
structure type this way:

1struct product {
2
3 int weight;
4
float price;

} apple, banana,
melon;

It is important to clearly differentiate between what is the structure type name, and what is an
object (variable) that has this structure type. We can instantiate many objects (i.e. variables, like
apple, banana and melon) from a single structure type (product).

Once we have declared our three objects of a determined structure type (apple, banana and
melon) we can operate directly with their members. To do that we use a dot (.) inserted between
the object name and the member name. For example, we could operate with any of these
elements as if they were standard variables of their respective types:

1apple.weight
2
3apple.price
4
5
banana.weigh
6
t

banana.price
melon.weight

melon.price

Each one of these has the data type corresponding to the member they refer to: apple.weight,
banana.weight and melon.weight are of type int, while apple.price, banana.price and
melon.price are of type float.

Let's see a real example where you can see how a structure type can be used in the same way as
fundamental types:

1// example about structures Enter title: Alien


2
3#include <iostream> Enter year: 1979
4
5
#include <string>
6
7
8#include <sstream> My favorite movie is:
9
10using namespace std; 2001 A Space Odyssey (1968)
11
12 And yours is:
13
14struct movies_t { Alien (1979)
15
16
17 string title;
18
19 int year;
20
21} mine, yours;
22
23
24
25
26void printmovie (movies_t movie);
27
28
29
30int main ()
31
32{
33
34
string mystr;
35
36
37
38
mine.title = "2001 A Space
Odyssey";

mine.year = 1968;
cout << "Enter title: ";

getline (cin,yours.title);

cout << "Enter year: ";

getline (cin,mystr);

stringstream(mystr) >>
yours.year;

cout << "My favorite movie is:\n


";

printmovie (mine);

cout << "And yours is:\n ";

printmovie (yours);

return 0;

void printmovie (movies_t movie)

cout << movie.title;

cout << " (" << movie.year <<


")\n";

}
Part-B

3. Consider the array NAME


i. NAME

4. Mary
5. Jane
6. Diana
7. Susan
8. Karen
9. Edith

(i)Write the Algorithm to delete an element from an array and also delete jane from the
array NAME.

Ans. Assuming you dynamically allocated this array using "new"...

delete array[arraysize - 1];

arraysize--;

(ii) Write the algorithm to INSERT an element in an algorithm and also add ‘ABC’ into
the array NAME.

5. An array A contain 20 positive integers .Write algorithm which find the number of
elements of A which are even ,and the number of elements of array which are odd.

Ans. #include<stdio.h>
#include<conio.h>

void main()

int a[20],even=0,odd=0,i;

for(i=0;i<19;i++)
{

if(a[i]%2==0)

printf("%d\t",a[i]);

even++;

else

printf("%d\t",a[i]);

odd++;

printf("Total no of Even found is=%d",even);

printf("Total no of Odd found is=%d",odd);

getch();

Algorithm for this>>

6. Write algorithm to multiply two matrixes

Ans.

Вам также может понравиться