Annexure Ii

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

ANNEXURE II

Homework No. : ___________________2__________________Course code:


CSE205

Course Instructor: Komal Mam Course Tutor (if applicable)


____________

Date of Allotment________________________Date of
Submission___19/3/10____

Student`s Roll No: B48 Section No.


RC2801

Declaration:

I declare that this assignment is my individual work. I have not


copied from any other student`s work or from any other source
except where due acknowledgment is made explicitly in the text,
nor has any part been written for me by another person.

Student`s
Signature _________________

Evaluator`s comments:

_____________________________________________________________________________
_

Marks obtained ______________________out


of___________________________________

Content of Homework should start from this page only:

1)
The main advantages of using pointers are
1.) 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.
2.) In the case of arrays, we can decide the size of th array at runtime by allocating the necessary
space.
3.) In the case of pointers to classes, we can use polymorphism and virtual classes to change the
behavior of pointers to various types of classes at runtime

the disadvantages of pointers


1.) If sufficient memory is not available during runtime for the storage of pointers, the program
may crash (least possible)
2.) If the programmer is not careful and consistent with the use of pointers, the program may
crash (very possible)

Pointer is useful in data structure in various ways such as it can be used as the link in the link list
and stack. Pointer and pointer array are used to facilitate the processing of the information in
data.

2)

The concept of “Fixed block storage allocation” and “Buddy system” in dynamic memory
management are as follows

1) Fixed-size-blocks allocation

Fixed-size-blocks allocation, also called memory pool allocation, uses a free list of fixed-size
blocks of memory (often all of the same size). This works well for simple embedded systems

2) Buddy blocks

In this system, memory is allocated from a large block in memory that is a power of two in size.
If the block is more than twice as large as desired, it is broken in two. One of the halves is
selected, and the process repeats (checking the size again and splitting if needed) until the block
is just large enough.

All the blocks of a particular size are kept in a sorted linked list or tree. When a block is freed, it
is compared to its buddy. If they are both free, they are combined and placed in the next-largest
size buddy-block list. (When a block is allocated, the allocator will start with the smallest
sufficiently large block avoiding needlessly breaking blocks)
3)

In computer science, dynamic 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.

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.

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. This block of functions can be replaced by the user with custom code
to implement a different memory management scheme. For example, an embedded system
application might want to use a fixed-sized static block from which to allocate.
The built-in memory management logic implements a nibble-allocation memory management
algorithm that provides superior performance to calling malloc and free directly. This algorithm
causes memory blocks to be allocated up front in larger sizes and then subsequently split up
when future allocation requests are received. These blocks can be reset and reused in
applications that are constantly allocating and freeing memory

4)

Malloc- The malloc() function shall allocate unused space for an object whose size in bytes is
specified by size and whose value is unspecified.

Calloc - The calloc() function allocates unused space for an array of nelem elements each of
whose size in bytes is elsize. The space is initialised to all bits 0.

Realloc- The realloc() function changes the size of the memory object pointed to by ptr to the
size specified by size. The contents of the object will remain unchanged up to the lesser of the
new and old sizes. If the new size of the memory object would require movement of the object,
the space for the previous instantiation of the object is freed. If the new size is larger, the
contents of the newly allocated portion of the object are unspecified. If size is 0 and ptr is not a
null pointer, the object pointed to is freed. If the space cannot be allocated, the object remains
unchanged.

Free - The free() function causes the space pointed to by ptr to be deallocated; that is, made
available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the
argument does not match a pointer earlier returned by the calloc(), malloc(), realloc() or valloc()
function, or if the space is deallocated by a call to free() or realloc(), the behaviour is undefined.
5)
Pointer to array is the pointer pointing to an array and in array to pointer array is pointing to
array. Pointer to array can be used in linklist whereas array to pointer is start.
If pointers can point to arrays, it seems only fitting that the reverse should be true. Arrays of
pointers are a type of array of particular interest. Just as arrays may contain other data types, an
array may contain pointers.This type of declaration isn’t used very often except in the case of an
array of pointers to character strings.

6)

Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted
from that memory location while pointer is still pointing such memory location. Such pointer is
known as dangling pointer and this problem is known as dangling pointer problem.

Initially:

Later:

For example:
int *call();
void main(){
int *ptr;
ptr=call();
clrscr();
printf("%d",*ptr);

}
int * call(){
int x=25;
++x;
return &x;
}
Far pointer = A far pointer is, in a Segmented architecture computer, a pointer which includes a
segment number, making it possible to point to addresses outside of the current segment.
Sometimes you can get away with using a small memory model in most of a given program.
There might be just a few things that don’t fit in your small data and code segments. When that
happens, you can use explicit far pointers and function declarations to get at the rest of memory.
A far function can be outside the 64KB segment most functions are shoehorned into for a small-
code model. (Often, libraries are declared explicitly far, so they’ll work no matter what code
model the program uses.)

A far pointer can refer to information outside the 64KB data segment. Typically, such pointers
are used with
farmalloc() and such, to manage a heap separate from where all the rest of the data lives. If you
use a small-data, large-code model, you should explicitly make your function pointers far.

7)

Array has a finite number were as in case of link list there is no boundation for such thing

In traversing we have to travers all element one by one where as in link list we can traverse
according to link available in the present link

In searching we have to search all element one by one where as in link list we can search
according to link available in the present link

8)

No, we cannot use binary search in link list because we cannot define the middle position in link
list and hence we cannot perform the binary search

You might also like