retagged by
9,460 views
17 votes
17 votes
Consider the following $\text{ANSI C}$ program:
#include <stdio.h>
#include <stdlib.h>
struct Node{
        int value;
        struct Node *next;};
int main( ) {
    struct Node *boxE, *head, *boxN; int index=0;
    boxE=head= (struct Node *) malloc(sizeof(struct Node));
    head → value = index;
    for (index =1; index<=3; index++){
        boxN = (struct Node *) malloc (sizeof(struct Node));
        boxE → next = boxN;
        boxN → value = index;
        boxE = boxN; }
for (index=0; index<=3; index++) {
    printf(“Value at index %d is %d\n”, index, head → value);
    head = head → next;
    printf(“Value at index %d is %d\n”, index+1, head → value); } }

Which one of the following statements below is correct about the program?

  1. Upon execution, the program creates a linked-list of five nodes
  2. Upon execution, the program goes into an infinite loop
  3. It has a missing $\textsf{return}$ which will be reported as an error by the compiler
  4. It dereferences an uninitialized pointer that may result in a run-time error
retagged by

2 Answers

Best answer
18 votes
18 votes

Lets see the first for loop:

for (index =1; index<=3; index++){
        boxN = (struct Node *) malloc (sizeof(struct Node));
        boxE -> next = boxN;
        boxN -> value = index;
        boxE = boxN; }

After this we get a linked list of size $4$ with head pointing to its beginning, and $\textsf{boxE}$ and $\textsf{boxN}$ pointing to the last node and the next pointer of the last node being uninitialized. 

Now the second for loop will do printing as follows until the second $\textsf{printf}$ of the final iteration.

  • Value at index 0 is 0
  • Value at index 1 is 1
  • Value at index 1 is 1
  • Value at index 2 is 2
  • Value at index 2 is 2
  • Value at index 3 is 3
  • Value at index 3 is 3

After this, the $\text{head}$ pointer being uninitialized will be having random content which gets treated as an address. So, when head -> value happens it is basically reading data from uninitialized memory location and so can result (not saying will result because by chance the uninitialized memory can be a valid location) in runtime error. 

To correct the error, we just have to add an extra line of code as given below:

for (index =1; index<=3; index++){
        boxN = (struct Node *) malloc (sizeof(struct Node));
        boxE -> next = boxN;
        boxN -> value= index;
    boxN-> next = NULL;
        boxE = boxN; }

Correct option: D

selected by
9 votes
9 votes
Answer is $(d)$.

The first loop of the code creates a linked-list of size $4$. Then in the second loop will print –

Value at index 0 is 0

Value at index 1 is 1

Value at index 1 is 1

Value at index 2 is 2

Value at index 2 is 2

Value at index 3 is 3

Value at index 3 is 3

After this, the $head$ pointer will point to some random address and tries to access $value$ within it assuming it as a struct of type $Node$, which results in an error specified in option $(d)$.
Answer:

Related questions