Programming and data structure miscellaneous
- Consider the following C code :
#include < stdio.h >
int *assignval (int *x, int val) {
*x = val;
return x;
}
void main () {
int *x = malloc (sizeof (int));
if (NULL == x) return;
x = assignval(x,0);
if (x) {
x = (int *) malloc (sizeof (int));
if (NULL == x) return;
x = assignval(x,0); if (x) { x = (int *) malloc (sizeof (int));
if (NULL == x) return; x = assignval (x, 10);
}
printf (“%d\n”, *x);
free (x);
}
The code suffers from which one of the following problems :
-
View Hint View Answer Discuss in Forum
Consider all options.
1. In option (a), we don’t need to cast the, result as void * is automatically and safely promoted to any other pointer type in this case. So it is not correct.
2. In option (b), it is discarded for obvious reason, so it is also not correct.
3. In option (c), the dangling pointer is nothing but the pointer which is pointing to non-existing memory. (deallocated or deleted memory) which is not happening here, so it is also not correct.
4. In option (d), when you are calling malloc second time, then new location is assigned to X and previous memory location is lost and now we do not have no reference to that location, resulting in memory leak, so option (d) is correct.Correct Option: D
Consider all options.
1. In option (a), we don’t need to cast the, result as void * is automatically and safely promoted to any other pointer type in this case. So it is not correct.
2. In option (b), it is discarded for obvious reason, so it is also not correct.
3. In option (c), the dangling pointer is nothing but the pointer which is pointing to non-existing memory. (deallocated or deleted memory) which is not happening here, so it is also not correct.
4. In option (d), when you are calling malloc second time, then new location is assigned to X and previous memory location is lost and now we do not have no reference to that location, resulting in memory leak, so option (d) is correct.
- Consider the following intermediate program in three address code
P = a – b
q = P * c
P = u * v
q = p + q
Which one of the following corresponds to a static single assignment form of the above code?
-
View Hint View Answer Discuss in Forum
Consider all options.
1. In option (a), p1 and q1 are initialized twice or used again for temporary storage, which is not allowed under static single assignment.
2. In option (b), there is no initialization again of the variables and all the statement are correct.
3. In option (c), the second line statement is not correct. It should be q1 = p1 * C.
And P2, P4, q3 are not initialized anywhere.
4. In option (d), the second line statements is not correct. It should be q1 = P1 * C So, option (b) is correct.Correct Option: B
Consider all options.
1. In option (a), p1 and q1 are initialized twice or used again for temporary storage, which is not allowed under static single assignment.
2. In option (b), there is no initialization again of the variables and all the statement are correct.
3. In option (c), the second line statement is not correct. It should be q1 = p1 * C.
And P2, P4, q3 are not initialized anywhere.
4. In option (d), the second line statements is not correct. It should be q1 = P1 * C So, option (b) is correct.
- Consider the C code fragment given below.
typedef struct node {
int data;
node* next;
} node;
void join (node* m, node* n){
node* p = n;
while (p – > next != NULL) {
p = p– > next;
}
p– > next – m;
}
Assuming that m and n point to Valid NULL-terminated linked lists, invocation of join will
-
View Hint View Answer Discuss in Forum
According to the given C program. It used two linked lists. After the code execution, list ‘m’ will be appended to the end of list ‘n’ due to pointer ‘p’. Move to last node of the list but in some cases it may dereference to null pointer.
Correct Option: B
According to the given C program. It used two linked lists. After the code execution, list ‘m’ will be appended to the end of list ‘n’ due to pointer ‘p’. Move to last node of the list but in some cases it may dereference to null pointer.
- Consider the following function :
int unknown (int n) {
int i, j, k=0;
for (i=n/2; i<=n; i++)
for (j=2; j<=n; j=j*2)
k = k + n/2;
return (k);
}
The return value of the function is
-
View Hint View Answer Discuss in Forum
The return value of the function is q (n2 log n)
The outer for loop goes n + 1 iterations. 2
The inner for loop runs independent of the outer loop.And for each inner iteration, n gets added to k. 2 ∴ n × #outer loops × #inner loops per outer loop. 2
#Inner loops = θ (log n) [∵ 2θ(log n) = θ (n)]∴ n × n + 1 . θ (log n) = θ (n2 log n) 2 2
Correct Option: B
The return value of the function is q (n2 log n)
The outer for loop goes n + 1 iterations. 2
The inner for loop runs independent of the outer loop.And for each inner iteration, n gets added to k. 2 ∴ n × #outer loops × #inner loops per outer loop. 2
#Inner loops = θ (log n) [∵ 2θ(log n) = θ (n)]∴ n × n + 1 . θ (log n) = θ (n2 log n) 2 2
- What is printed by the following C program?
int f (int x, int * py, int ** ppz)
{
int y, z;
**ppz + = 1; z = *ppz;
*py + = 2; y = *py;
x + = 3;
return x + y + z;
}
void main ()
{
int c, *b, **a,
c = 4; b & c; a = & b
printf(“%d”, f(c, b, a));
}
-
View Hint View Answer Discuss in Forum
The program gets executed in the following manner
Graphical Representation
Now, considering
int y, z;
**ppy + = 1; z = *ppz = 6
*py + = 2; y = *py = 6
x = 4 + 3 = 7
return x + y + z;
and
c = 4; b & c; a = &b;
printf (“%d”, f(c, b, a)),
From the code,
The output is printed as 6 + 6 + 7 = 19.Correct Option: B
The program gets executed in the following manner
Graphical Representation
Now, considering
int y, z;
**ppy + = 1; z = *ppz = 6
*py + = 2; y = *py = 6
x = 4 + 3 = 7
return x + y + z;
and
c = 4; b & c; a = &b;
printf (“%d”, f(c, b, a)),
From the code,
The output is printed as 6 + 6 + 7 = 19.