Aricent Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
The third option in addition to 2 is also right
struct node* m = n;
free( n );
n = m->next;
Sure. Both 3 and 5 free the memory the pointer points to before attempting to use that memory. Creating a backup of the pointer doesn't help because the backup still points to the same memory location. It's not the pointer that gets altered during a call to free() -- it's the memory it points to. 2 is the correct option, assuming we correct the first line to use a *: struct node* m = n;
Actually, trick question. They're all wrong. Option 2 needs a star:
struct node* m = n;
n = n->next;
free( m );
choice 4 . is correct
because n is a pointer .. first freeing it and then assigning it a new address .. just like we do in deletion of a linked list .
Choice 1 is wrong as it is not saving the next node value .. and choice 2,3,5 are illogical as there is no point in assigning a pointer to a normal struct variable for memory freeing purpose only .
2
- A June 27, 2012