Advantages And Disadvantages Of Using A Pointer Computer Science Essay

Published:

Write advantages and disadvantages of using pointer. How the concept of pointers is useful in the implementation of data structures?

A pointer allows a function or a program to access a variable outside the preview function or a program ,using pointer program can access any memory location in the computer's memory.

2)since using return statement a function can only pass back a single value to the calling function, pointers allows a function to pass back more than one value by writing them into memory locations that are accessible to calling function.

3)Use of pointer increases makes the program execution faster

4)using pointers, arrays and structures can be handled in more efficient way.

5) without pointers it will be impossible to create complex data structures such as linked list , trees, and graphs.

Disadvantages of pointers:-

1)we can access the restricted memory area.

Lady using a tablet
Lady using a tablet

Professional

Essay Writers

Lady Using Tablet

Get your grade
or your money back

using our Essay Writing Service!

Essay Writing Service

2) Pointers require one additional dereference, meaning that the final code must read the variable's pointer from memory, then read the variable from the pointed-to memory. This is slower than reading the value directly from memory.

3). If sufficient memory is not available during runtime for the storage of pointers, the program may crash

When setting up data ststructures like lists, queues and trees, it is necessary to have pointers to help manage how the structure is implemented and controlled.Pointers and Structures can be used to build data structures that expand and shrink during execution examples stack queues,trees etc.While pointer has been used to store the address of a variable,it more properly applies to data structures whose interface explicitly allows the pointer to be manipulated as a memory address.Because pointers allow largely unprotected access to memory addresses.

2). Elaborate the concept of "Fixed block storage allocation" and "Buddy system" in dynamic memory management.

Sol.

Fixed block storage allocation:- This is the simplest storage maintenance method. Here each block is of the same size. The size is determined by the system manager. Here the memory manager maintain a pointer ' AVAIL' which points a list of non contiguous memory blocks.

A user program communicate with the memory manager by means of two function GETNODE(NODE) and RETURNNODE(PTR).The procedure GETNODE is to get a memory block to store data of type NODE. This procedure when invoked by a program returns a pointer to first block in the pool of restorage. The AVAIL then points to the next block.If avail=NULL it indicates no more memory is available for allocation. Similarly whenever a memory block is no more required it can be returned to the memory bank through a procedure RETURN NODE().

Buddy system:- It is the another storage management system which restricts the size of blocks to some fixed set of sizes. These blocks of restricted sizes are maintained in a linked list. Whenever a request for a block of size N comes, the number M the smallest of the fixed sizes but equal to or largest than N, is determined and a block of size M is allocated if available on the list. If not available then a larger block if available is split into two sub-blocks known a s buddies. Each of them are also of fixed sizes and the process is repeated until a block of size M is produced. for example , if k=1 and Fo=8, then the block sizes are 8,16,32,64,128,…. THAT is ,the block sizes are successive powers of 2; and the buddy system based on such fixed sizes is called binary buddy system.

3.)Differentiate between static memory allocation and dynamic memory allocation. Illustrate various memory management functions

Sol.

In case of static storage management scheme , the net amount of memory required for various data for a program is allocated before the starting of the execution of a program once memory is allocated, it neither can be extended nor can be returned to the memory bank for the use of other programs at the same time. On the other hand dynamic storage management schemes allows user to allocate and deallocate as per necessity during the execution of programs.

Lady using a tablet
Lady using a tablet

Comprehensive

Writing Services

Lady Using Tablet

Plagiarism-free
Always on Time

Marked to Standard

Order Now

The static storage allocation is easy to implement and efficient from execution point of view .Here all variables those are required for a program is allocated during compile time this is why static storage allocation is a compile time phenomena. In this each subprogram/subroutine of a program is compiled separately and the space required for them is reserved till the execution of the program. On the other hand dynamic memory allocation , space for memory variables is allocated dynamically that is as per the current demand during the execution. When a subprogram is invoked space for it is allocated and space is returned when the subprogram completes its execution. Thus , the space required to run a program is not fixed as in static allocation, rather it varies as program execute.

Various memory management functions:-

1)malloc():-The malloc function dynamically allocates memory from heap.The prototype for malloc() function is

Void *malloc(size_t size);

2)calloc():- The calloc() function dynamically allocates memory automatically initializes the memory to zeroes. The prototype for calloc() function is

Void *calloc(size_t nitems , size_t size);

It takes two arguments . The first argument is the number of elements and the second argument is the size of each element.

3) realloc():- The realloc() function changes the size of previously dynamically allocated memory with malloc(), calloc(), realloc() functions.The prototype for realloc() function is

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

It takes two argument . the first argument is the pointer to the original object and the second argument is the new size of the object.

4.)Write different ways to manage records in memory

Sol.) Since records may contain non homogeneous data, the elements of a record cannot be stored in an array . Some programming languages such as PASCAL and COBOL do have record structures built into the language.

Suppose a programming language does not have available the hierarchical structures that are available in PASCAL and COBOL . Assuming the record contains non homogeneous data , the record may have to be stored in individual variables , one for each of its elementary data items. On the other hand one wants to store an entire file of records, such a file may be stored in memory as a collection of arrays that is, where elements in different arrays with the same subscript belonging to the same record.

Part-B

1.)Illustrate the use of array of pointers and pointers to an array

Sol.)

An array of pointers is that for eg if we have array of 10 int pointers ie int *a[10] then each element that which is stored in array are pointed by pointers. here we will have ten pointers. In pointer to an array for eg int(*a)[10] here all the elements that is all the ten elements are pointed by a single pointer.

int *a[10]:-array of pointers.

consider one array int b[10]={1,2,3,4,5,6,7,8,9,0};so

elements will be stored in addresses .now this address are

stored in array of pointers.thats

int *a[10]={b+0,b+1,b+2,b+3,b+4,b+5,b+6,b+7,b+8,b+9};means a+0=address of value 1 is the first element of int

*a[](first element of int b[10])and so on.

while int(*a)[10]:-here a is an pointer to an array containing 10

integers.

suppose int b[10];

then a=&b[10];

this will give element of int b[10] array thats b[0];and so on but in case of two dimensional array first we have to allocate base address of respective one dimensional array and base address of element of one dimensional array then only we can use pointer to an array.

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

Sol.)

A far pointer uses both the segment and the offset address to point to a location in memory  The far pointer can point to any location in memory. . Far pointers have a size of 4 bytes . They store both the segment and the offset of the address the pointer is referencing. A far pointer has an address range of 0 - 1M bytes. A far pointer can be incremented and or decremented Only the offset of the pointer is actually incremented or decremented. The segment is never incremented by the arithmetic operators.On the other hand Dangling pointers are the pointers that do not point tao a valid object of the appropriate type. These pointers arise when an object is deleted or deallocated,without modifting the value of the pointer so that pointer stll points to the memory location of deallocated memory .As the system may reallocate the previously freed memory to another process ,if the original program then derefrences the dangling pointer,results in bugs or errors as the memory may contain completely different data. 

Lady using a tablet
Lady using a tablet

This Essay is

a Student's Work

Lady Using Tablet

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Examples of our work

Consider the following example

{

char *cp = NULL;

{

char c;

cp = &c;

}  

/* cp is now a dangling pointer */

}

Solution to dangling pointer:

char *cp = malloc ( A_CONST );

free ( cp ); /* cp now becomes a dangling pointer */

cp = NULL; /* cp is no longer dangling */

Differentiate between linked list and arrays in terms of representations, traversal and searching.

Sol.)

1)In case of array traversing is used visiting all elements in an array while to traverse a single linked list we mean to visit every node in the list starting from first node to last node.

2).Searching operation in an array is applied to search an element interest in an array.It is a process of finding the location of given element in an array.The search is said to be successful if the given element is found.there are two types of search operation :

Linear search

Binary search

If the array elements are in random order then one have to use linear search technique and if the array elements are sorted then it is preferable to choose binary search technique.While in case of linked list only linear searching is possible.This is one of the limitation of linked lists as there is no way to find the location of the middle element of the list.

can we perform binary search in linked list ,if no then illustrate the reason.

Sol.)

No, we cannot perform binary search in linked list because there is no way

Of indexing the middle element in the list. With a sorted linear array we can apply a binary search whose running time is proportional to log2n. On the other hand a binary search algorithm cannot be applied to a sorted linked list, since there is no way of indexing the middle element in the list. This property is one of the main drawbacks in using a linked list as a data structure.