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

Part-A

Write advantages and disadvantages of using pointer. How the


concept of pointers is useful in the implementation of data
structures?

Answer:-The main advantages of using pointers are


1.) In the case of arrays, we can decide the size of th array at runtime by allocating
the necessary space.

2)Function cannot return more than one value. But when the same function can
modify many pointer variables and function as if it is returning more than one
variable.
Disadvantage:1.If sufficient memory is not available during runtime for the storage of
pointers, the program may crash (least possible).

Pointers are used extensively in data structures like linked lists, all kinds of trees
and hierarchies, etc. I will not discuss these here. Suffice it to say that such
advanced structures could not be done without pointers or references, not even in
languages which officially don't even use pointers, like Java (well, as far as I know).
If you really want to know more about such structures, you'll have to read one of
the many textbooks on that subject.

Elaborate the concept of “Fixed block storage allocation” and


“Buddy system” in dynamic memory management.

Answer:- Fixed-blocks storage allocation means the block that is assign fix size is called the
fixed block storage allocation it is also called it is also called pool allocation, it uses a the list that
is free, it is use fixed blocks size memory..This is very simple.
Buddy system:
The system in which the memory is allocated in the big blocks and the size of that blocks is 2n
then it is called the buddy system. The block is broken when the block that is allocated is large
than it should be then the block is broken in two parts. The all blocks are in the link list . these
has the particular size. The block is compared its buddy only when the block is free..they are
combine when both they are free If they are both free

Differentiate between static memory allocation and dynamic


memory allocation. Illustrate various memory management
functions
Static memory allocation: In static memory allocation the compiler allocates the required
memory space for a declared variable.By using the address of operator,the reserved address is
obtained and this address may be assigned to a pointer variable.Since most of the declared
variable have static memory,this way of assigning pointer value to a pointer variable is known as
static memory allocation. memory is assigned during compilation time.

Dynamic memory allocation: Dynamic memory allocation uses functions such as malloc( ) or
calloc( ) to get memory dynamically.If these functions are used to get memory dynamically and
the values returned by these functions are assingned to pointer variables, such assignments are
known as dynamic memory allocation.memory is assined during run time.

Memory management functions: handle the allocation and deallocation of dynamic memory. These
functions form an abstraction layer above the standard C memory management functions malloc, free,
and realloc.,calloc,coreleft.

1.malloc():This function is used to allocate memory space in bytes to the the variables od different data
types.This function reserves bytes of determine size and returns the base address to pointer variable.

2.calloc():This function is useful for allocating multiple blocks of memory.It is declared with two
arguments.

3.free(): function used to releases the memory allocated by memory allocating functions.Thus,using this
function wastage of memory is prevented.

4.realloc():This function reallocates main memory.

5.coreleft():This function returns a measure of unused memory

4. Write different ways to manage records in memory

Answer. We have to manage the record so that we can get back when ever we
want. There are many ways to manage the records:
• Array: In the c the array store the same kind of elements. With the help of
the array we can store the data of the same type.

• Pointer: the pointer is the variable that can store the address of the another
variable. It is also helpful for the storing the data. It manage the records.

• File: The file system is also helpful to manage record. We store the
information in the file. The all info can be kept by the file.
Part-B
Illustrate the use of array of pointers and pointers to an array.

Answer: An array of pointers is just that, an array in which the variables are pointers.
In C this would be an array of pointer variables that are each 4 bytes in size. It is
declared like this:

int *pointers[3];

A pointer to an array is a pointer that points to the whole array. For example, in C if
you have
int numbers [5] [10];
int (*pointer to Array)[10] = numbers + 2;

pointer to Array points to the third element of numbers, which is itself an array.

Give example to show the use of far pointer and dangling pointer
problems.

Answer. Use of far pointer In a segmented architecture computer, a far pointer is a pointer
which includes a segment selector, making it possible to point to addresses outside of the current
segment.

For example, in an Intel 8086, where an ordinary pointer is just a 16-bit offset within an implied
segment, a far pointer has two parts: a 16-bit segment value and a 16-bit offset value. A linear
address is obtained by shifting the binary segment value four times to the left, and then adding
the offset value. Hence the effective address is 20 bits (actually 21-bit, which led to the address
wraparound and the Gate A20). Comparison and arithmetic on far pointers are problematic: there
are potentially 4096 different segment-offset address pairs that point to the same address. To
compare two far pointers, they must first be converted (normalized) to their 20-bit linear
representation.

On C compilers targeting the 8086 processor family, far pointers were declared using a non-
standard far qualifier. For example, char far *p; defined a far pointer to a char. The difficulty of
normalizing far pointers could be avoided with the non-standard huge qualifier.
Use of dangling pointer: pointers that do not point to a valid object of the appropriate type.
Dangling pointers arise when an object is deleted or deallocated without modifying the value of
the pointer so that the pointer still points to the memory location of the deallocated memory. As
the system may reallocate the previously freed memory to another process if the original
program then dereferences the (now) dangling pointer unpredictable behavior may result as the
memory may now contain completely different data. This is especially the case if the program
writes data to memory pointed by a dangling pointer as silent corruption of unrelated data may
result leading to subtle bugs that can be extremely difficult to find or cause segmentation faults
(*NIX) or general protection faults (Windows).

Differentiate between linked list and arrays in terms of


representations, traversal and searching.

Answer. The main difference between the linked list and the array is that while the array is a
static data structure (with fix number of elements). On the other hand the linked list - dynamic
data structure.
In terms of complexity, the linked list is usually more efficient as to the space it uses, however,
algorithms for linked lists are usually more complicated that those of the array.

Array vs. Linked List representation in memory:

Traverse:It is the operation to visit each node at once.


Search: It is the operation to search the given node with a given key value.

can we perform binary search in linked list ,if no then illustrate the
reason.
Answer. No we cannot perform binary search in linked list because we will
have to write our own, possibly inefficient algorithm to get the value of the
middle node of a linked list. In a linked list, we loosse the ability to get the
value of any node in a constant time.
One solution to the inefficiency of getting the middle of the linked list during
a binary search is to have the first node contain one additional pointer that
points to the node in the middle. Decide at the first node if we need to check
the first or the second half of the linked list. Continue doing that with each
half-list.

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