You are on page 1of 8

BSCS-402 DATA STRUCTURE

LAB 5
OBJECT 1:DANGLING POINTER PROBLEM: What is the output of the
following piece of code? Explain your answer

S.no C CODE
1 /* File: programs/c/DanglingPointers.c */
2 #include #include
3 typedef struct { /* define a structure */
4 int x; /* ... with an x field */
5 int y; /* ... and a y field */
6 } Coordinate_t; /* ... called Coordinate_t */
7 int main() {
8 /* Allocate a pointer to a coordinate */
9 Coordinate_t *p;
10 p = (Coordinate_t*)malloc(sizeof(Coordinate_t));
11 /* Use p */
12 p->x = 256; /* Or: (*p).x = 256; */
13 p->y = 512; /* Or: (*p).y = 512; */
14 printf("p->x is %d\n", p->x); /* "p->x is 256" */
15 printf("p->y is %d\n", p->y); /* "p->y is 512" */
16 /* Deallocate p */
17 free(p);
18 /* Erroneous attempt to use p after deallocation */
19 printf("p->x is %d\n", p->x); /* "p->x is 0" */
20 printf("p->y is %d\n", p->y); /* "p->y is 512" */
21 /* Allocate another pointer to a coordinate */
22 Coordinate_t *p2;
23 p2 = (Coordinate_t*)malloc(sizeof(Coordinate_t));
24 /* Erroneous attempt to use p2 before initialisation */
25 printf("p2->x is %d\n", p2->x); /* "p2->x is 0" */
26 printf("p2->y is %d\n", p2->y); /* "p2->y is 512" */
27 /* Update p2 */
28 p2->x = 1024;
29 /* Erroneous attempt to use p after deallocation */
30 printf("p->x is %d\n", p->x); /* "p->x is 1024" */
31 printf("p->y is %d\n", p->y); /* "p->y is 512" */
32 exit(0);
33 }
Outcome:
First run:

Another run

1. After implementing free at line (17) Machine producing different


outcome
2. Line (3) to (7) Block-structure, When compiler comes out of function
(Block-Structure) it destroys data inside so it has no use outside of
scope hence if you point any local variable while working it may point
non-existent content , so it becomes a dangling pointer.
3. When memory is lost and cannot be reclaimed we term this a space
leak.
OBJECT 2: Array out-of-bounds violations in C: Identify the error in
this program

S.no C CODE
1 /* File: programs/c/ArrayViolation.c */
2 #include <stdio.h>
3 #include <malloc.h>
4 int main() {
5 /* An array of four integers */
6 int* squares = (int*) malloc (4 * sizeof(int));
7 int i;
8 for (i = 1 ; i <= 4; i++) /* initialise the array */
9 squares[i] = i * i;
10 for (i = 1 ; i <= 4; i++) /* print the contents */
11 printf("%d\n", squares[i]);
12 return(0);
13 }

Outcome :
Program runs flawlessly and will not produce any error at run time
but in C Indexing starts from [0] hence accessing [4] will result out of
bounds

This error is known as off by one error .


OBJECT 3: What values are returned during the following series of
stack operations, if executed upon an initially empty stack?

push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9),


push(1), pop(), push(7), push(6), pop(), pop(), push(4), pop(), pop()

S.no PYTHON CODE


1 def Stack ():
2 stack = []
3 while True:
4 selection = int(input("PUSH:1 POP:2 STACK:3 Exit:4 : "))
5 if selection == 1:
6 var = int(input("Push: "))
7 stack.append(var)
8 elif selection == 2:
9 stack.pop(len(stack)-1)
10 elif selection == 3:
11 List_to_Stack = []
12 List_to_Stack = stack.copy()
13 List_to_Stack.reverse()
14 for i in List_to_Stack:
15 print ("|",i,"|")
16 elif selection == 4:
17 break
18 else:
19 continue

Output:
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 5
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 3
PUSH:1 POP:2 STACK:3 Exit:4 : 3
|3|
|5|
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 3
|5|
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 2
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 8
PUSH:1 POP:2 STACK:3 Exit:4 : 3
|8|
|2|
|5|
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 9
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 1
PUSH:1 POP:2 STACK:3 Exit:4 : 3
|1|
|9|
|5|
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 3
|9|
|5|
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 7
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 6
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 1
Push: 4
PUSH:1 POP:2 STACK:3 Exit:4 : 3
|4|
|9|
|5|
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 2
PUSH:1 POP:2 STACK:3 Exit:4 : 3
|5|
OBJECT 4: Implement a function with signature transfer(S, T) that transfers all elements
from stack S onto stack T, so that the element that starts at the top of S is the first to be
inserted onto T, and the element at
the bottom of S ends up at the top of T.

S.no PYTHON CODE


1 def transfer(S,R):
2
3 L = S.copy()
4 L.reverse()
5 for i in L:
6 R.append(i)
7 print("STACK:")
8 for i in L:
9 print("|", i, "|")
10
11 R.reverse()
12 print("\n")
13 print("STACK TRANSFER :")
14 for j in R:
15 print("|", j, "|")
16
17
18
19

transfer([1,8,5,4,1,10,2],[])
Output:

You might also like