Explanation : A) is wrong. We don’t need to cast the result as void * is automatically and safely promoted to any other pointer type in this case.
B) It is discarded for obvious reason.
C) is wrong, because dangling pointer is nothing but the pointer which is pointing existing memory (deallocated or deleted memory) which is not happening here.
D) is the answer. When you are calling malloc second time, new location is assigned to x and previous memory location is lost and now we don’t have no reference to that location resulting in memory leak.
Explanation : In given function foo every time in the while foo is called value 3 because val is passed with post decrement operator so the value 3 is passed and val is decremented later. Every time the function is called a new variable is created as the passed variable is passed by value, with the value 3. So the function will close abruptly without returning any value.
In the function bar, in the while loop value the value of val variable is not decrementing, it remains 3 only. Bar function in the while loop is called with val-1 i.e 2 but value of val is not decremented, so it will result in infinite loop.