Bscs-402 Data Structure Lab 5: Object 1
Bscs-402 Data Structure Lab 5: Object 1
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
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
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.
transfer([1,8,5,4,1,10,2],[])
Output: