why null==x. is a valid statement

Consider the following C code:

#include <stdio.h>

int * assignval (int *x, int val)
{
    *x = val;
    return x;
}

int 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, 10);
    }
    printf("%d\n", *x);
    free(x);
}

The code suffers from which one of the following problems:

A

compiler error as the return of malloc is not typecast appropriately.

B

compiler error because the comparison should be made as x==NULL and not as shown.

C

compiles successfully but execution may result in dangling pointer.

D

compiles successfully but execution may result in memory leak.

8Comments
Kavya Kamish Upadhyay @kavya8755
6 May 2017 02:06 pm

compiler error because the comparison should be made as x==NULL and not as shown.

shweta @shweta1920
6 May 2017 03:21 pm

@kavya8755 exActly..... but in gate2017 .. I tick this only .. option b .. but answer is not option b

Kavya Kamish Upadhyay @kavya8755
6 May 2017 04:07 pm

Answer is (D). sorry for that

 

 

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

to non-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.

shweta @shweta1920
6 May 2017 04:27 pm

@kavya8755 why option b is wrong plz explain

Shreyans Dhankhar @shreyans
7 May 2017 03:29 am

NULL == X is just another coding style used by the programmers to avoid some mistakes in codes. 

Whenever people write bigger codes they use to follow these type of convention instead of X == NULL reason being while writing code the programmer may type X = NULL instead of X == NULL which will result in overwriting of X.
While in other case NULL ==X if accidently programmer writes NULL = X then he/she will get a compilation error instead overwriting. 

shweta @shweta1920
13 May 2017 11:45 am

oh!!!..thanks.. 

Mithlesh Upadhyay @mithlesh
8 May 2017 02:27 am

There is nothing wrong with given code. If you compile and run this code you will get some output.

The problem is only that you have allocated memory(using malloc), but you have forgotten to deallocate it. This situation is known as memory leak problem. That should be deallocated after using memory block at run time.

int *x = malloc(sizeof(int));

See reference: http://c-faq.com/malloc/mallocnocast.html

Dangling pointer situation occurs when a pointer is pointing a memory location that has been deleted (using free). However, there is no `free` in given code. 
See difference: http://stackoverflow.com/questions/13132798/difference-between-dangling-...
Therefore, option (d) is correct.

shweta @shweta1920
13 May 2017 11:55 am

i did not get what is memory leak now .... by the link u sent  http://stackoverflow.com/questions/13132798/difference-between-dangling-...