Sie sind auf Seite 1von 6

HOMEWORK NO:2

CAP204: Fundamentals of Data Structures

Part-A

Ques 1) How debugging pointers are beneficial in data


structures?

Ans) Data in a computer is organized in a number of ways either


in logical or mathematical order. Such organization of data is
called data structures. Pointers are the memory variables that
points to the address of a variable.

Debugging refers to a process in software development whereby


program analysts comb through computer code looking for
“bugs” — the source of errors, flaws or security holes in the
internal program instructions. Debugging pointers are beneficial
in data structures as it would help us to remove the errors in
easier way without letting the programmer change the whole
program. One could see the line or the pointer where error is
occurring and thus compilation becomes easier.

Ques 2) How memory is allocated and deallocated


dynamically? Illustrate various functions for it?

Ans) Dynamic memory allocation (also known as heap-based


memory allocation) is the allocation of memory storage for use in
a computer program during the runtime of that program. It can
be seen also as a way of distributing ownership of limited
memory resources among many pieces of data and code.

Dynamically allocated memory exists until it is released either


explicitly by the programmer, or by the garbage collector. This is
in contrast to static memory allocation, which has a fixed
duration. It is said that an object so allocated has a dynamic
lifetime.

• Static memory allocation: space for the object is provided in


the binary at compile-time; these objects have an extent (or
lifetime) as long as the binary which contains them is loaded
into memory
• Automatic memory allocation: temporary objects can be
stored on the stack, and this space is automatically freed
and reusable after the block in which they are declared is
exited
• Dynamic memory allocation: blocks of memory of arbitrary
size can be requested at run-time using library functions
such as malloc from a region of memory called the heap;
these blocks persist until subsequently freed for reuse by
calling the library function free

Garbage collection (GC) is a form of automatic memory


management. It is a special case of resource management, in
which the limited resource being managed is memory. The
garbage collector, or just collector, attempts to reclaim garbage,
or memory occupied by objects that are no longer in use by the
program.

Garbage collection is often portrayed as the opposite of manual


memory management, which requires the programmer to specify
which objects to deallocated and return to the memory system
Static memory allocation refers to the process of allocating
memory at compile-time before the associated program is
executed, unlike dynamic memory allocation or automatic
memory allocation where memory is allocated as required at run-
time.

Various functions are

1) Malloc: malloc is a subroutine provided in the C and C++


programming language's standard libraries for performing
dynamic memory allocation. The malloc function is one of the
functions in standard C to allocate memory. Its function prototype
is

Void *malloc (size_t size);

This allocates size bytes of memory. If the allocation


succeeds, a pointer to the block of
memory is returned, otherwise a NULL pointer is returned. Upon
successful allocation, malloc returns a void pointer (void *),
which indicates that it is a pointer to a region of unknown data
type.

2) Free: Memory allocated via malloc is persistent: it will continue


to exist until the program terminates or the memory is explicitly
deallocated by the programmer (that is, the block is said to be
"freed"). This is achieved by use of the free function. Its prototype
is

Void free (void *pointer);

This releases the block of memory pointed to by pointer.


Pointer must have been previously returned by malloc, calloc,
or realloc and must only be passed to free once.

3) Calloc: An alternative is to use the calloc function, which


allocates memory and then initializes it. Its prototype is

Void *calloc(size_t elements, size_t element Size);

This allocates a region of memory, initialized to 0, of size


n elements × element Size.

4) Realloc: It is often useful to be able to grow or shrink a block


of memory. This can be done using realloc which returns a pointer
to a memory region of the specified size, which contains the same
data as the old region pointed to by pointer (truncated to the
minimum of the old and new sizes). If realloc is unable to resize
the memory region in place, it allocates new storage, copies the
required data, and frees the old pointer. If this allocation fails,
realloc maintains the original pointer unaltered, and returns the
null pointer value. The newly allocated region of memory is
uninitialized (its contents are not predictable). The function
prototype is

Void *realloc (void *pointer, size_t size);

Realloc behaves like malloc if the first argument is


NULL.

Ques 3) Pointers play important role in


implementation of various data structures .give your
views in this regard

Ans)

Pointers are basically the same as any other variable. However,


what is different about them is that instead of containing actual
data, they contain a pointer to the memory location where
information can be found. This is a very important concept, and
many programs and ideas rely on pointers as the basis of their
design, linked lists for example

They are used to facilitate the processing of information in data

Part-B

1. Consider the array NAME


i. NAME

2. Mary
3. Jane
4. Diana
5. Susan
6. Karen
7. Edith

(i)Write the Algorithm to delete an element from an


array and also delete jane from the array NAME.

Ans)

1. Del(info ,link, start, avail, jane, locp) this algorithm deletes


jane from array name and locp is the location which
precedes n nad locp= null
2. If locp= null then;

Set start:=link[start]. Deletes element

Else set link[locp]:=link[loc] deletes element

End if

3. Return delete to avail list

Set link[loc]:=avail and avail:=loc

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

Ans)

1. Insfirst (info, link, start, avail, abc)

This algorithm will insert abc as the first element of given


array

2. Overflow if avail =null, then overflow, and exit


3. Remove first element from given list

Set new: =avail and avail:=link[avail]

4. Set info[new]:= abc


5. Set link[new]:=start
6. Set start :=new
7. Exit

Ques2) 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)

1. Let a be an array of 20 integers


2. Take variables I, odd, even
3. Enter elements of the array
4. Check if (a==0) then

Display numbers are even and the numbers that is i

Else display number are odd and numbers

5. Repeat step 4
6. Exit

Oues 3) write an algorithm to multiply two matrices?

Ans) Step1) Let a and b be two matrices

Step 2) Enter order of matrices a and b say 3*3


Step3) Enter the elements of matrices a and b

Step 4) Print a[i][j]*b[i][j]

Step5) Exit

Das könnte Ihnen auch gefallen