Monday, September 28, 2009

Lab #2

CSC-260 Data Structures
Lab No.2
Dynamic Memory Allocation and De
allocation

Need of Dynamic Memory Allocation:
We often don't know
how much space we will need to store things at "compile time". Dynamic memory
allocation is the allocation of memory at "run time". With dynamic memory
allocation, while the program is running, the program requests more memory from
the computer. If there's enough memory available, the computer will grant the
program the right to use the amount it requests. After the dynamically allocated
space has been used it must be de-allocated.

Pointers and Dynamic Memory
Allocation:
Pointers come into play when we want to allocate and de-allocate
a chunk of memory while our program is running. Look at the following code.
int *ptr= new int ;
The new operation allocates space in the free store
(sometimes called the heap), which is a special region in the main memory area
dedicated to your program. The new operation assigns the amount of space needed
by the type of variable mentioned after it, in this case an int. If the new
succeeds, it returns a pointer to the allocated space. If it fails, it returns a
value of NULL.
One then uses *ptr, anytime access is needed to the space
pointed to. When one is finished with dynamically allocated space, one should
return it to the free store by using the delete operation as shown below. This
allows the space to later be reused for something else.
delete ptr;
Although dynamically allocated space should ideally be freed up when your
program ends, this does not always happen. A program that doesn't return all of
the space that it allocates is said to suffer from memory leaks. After running
the program one has less available memory than before, at least until the
computer is restarted.
You can also use ptr (in this example) as if it were
an array, because for all practical purposes it is! (The main difference is that
an ordinary array is kept in a different place in main memory than a dynamically
allocated array.) Remember that an array name is seen as a pointer to the first
data item, and that is precisely what ptr is. Thus you can use ptr[n] to get at
the item at index n in the array.
One again uses the delete operation to
free up space when finished with the allocated memory. However, with an array
the [] brackets are given between the delete and the pointer variable as shown
below:
delete [] ptr;

Dangling Pointer:
Dangling Pointer is a
pointer pointing to either unallocated memory area or to the area that has
already been freed. Dangling pointers arise when an object is deleted or
de-allocated, without modifying the value of the pointer, so that the pointer
still points to the memory location of the de-allocated memory. As the system
may re-allocate 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.A straight
forward example of dangling pointer is.

char *cp = NULL;

cp =
new char;
delete cp;

/* cp is now a dangling pointer as we have not
put it to NULL*/

To avoid a pointer becoming dangling, we can use NULL
reference.

Lab Tasks:

1) Write a program named “expandable
array”. Make a function “grow” which takes the old and new size of the array as
input and return a pointer to the new array. The program should take the number
of elements in the array from the user each time and dynamically allocate enough
memory until he decides to quit. The user should input the numbers in the array
each time and display them to show that expandable array works properly.
2)
Write the following code in the main method.
int *ptr = new int;
*ptr=5;
delete ptr;
cout<<*ptr<What is the problem in the above
code and how can it be solved?

3) Read the following code.
char
*string;
string = new char[20];
string = new char[30];
delete []
string;
What is the problem in the above code and how can it be solved?


4) Write a program that declares a structure named employee as
follows.
struct employee
{
char name [20];
int age;
float
income;
};
Now make an array of the employee structure of desired length
from the user using dynamic memory allocation. Print the data of all employees
and release the memory locations. Then ask if the user wants to perform data
entry and retrieval again.

No comments:

Post a Comment