edited by
35,418 views
87 votes
87 votes

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

The code suffers from which one of the following problems:

  1. compiler error as the return of $malloc$ is not typecast appropriately.
  2. compiler error because the comparison should be made as $x == \text{NULL}$ and not as shown.
  3. compiles successfully but execution may result in dangling pointer.
  4. compiles successfully but execution may result in memory leak.
edited by

7 Answers

11 votes
11 votes

Option B is plain stupid. Ignore it.

Option A isn't necessary, because programming language C does implicit typecasting.


So, we're left with Options C and D

Now, dangling pointer is (using) a pointer that points to nowhere / garbage value.

Memory leak is a piece of memory in the heap, that can't be accessed because the pointer for it is deleted off.

Now, see the code. Are we clearing a pointer? Yes, we are.

Here: 

    free(x);

After doing so, we're terminating the program, and not using the pointer x again. We'll never access x, which currently is freed (points to nowhere). So, no dangling pointer.

 

Is there a memory leak?

To immediately detect a memory leak in the code, try to find two successive malloc() assignments to a pointer, without clearing the pointer first.

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

We declared a pointer, and initialized it with a heap-address.

Then, we did this inside the if-statement

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

New memory location assigned to x, and x points to it now. Nothing points to the previous location, hence memory leak

 

Option D.


To avoid the memory leak, we should've had freed the pointer first, and assigned a new heap address using malloc() afterwards.

ie, malloc() then malloc()  ✕

malloc() then free() then malloc() ✓

edited by
0 votes
0 votes

The code suffers from option D. It compiles successfully but execution may result in a memory leak.

  • The `main()` function starts by allocating memory for an integer pointer `x` using `malloc(sizeof(int))`. If the allocation fails (i.e., `x` is `NULL`), the function returns without freeing any memory.

 

  • The `assignval()` function takes an integer pointer `x` and an integer value `val` as parameters. It assigns the value `val` to the memory location pointed to by `x` and returns `x`. This function does not cause any memory leaks.

 

  • In the `main()` function, after allocating memory for `x`, it calls `assignval()` to assign the value 0 to the memory location pointed to by `x`. This is correct and does not cause any issues.

 

  • The code then checks if `x` is not `NULL` and enters the `if` statement. Inside the `if` statement, it allocates memory for another integer pointer `x` using `malloc(sizeof(int))`. However, this overwrites the previous value of `x` and causes a memory leak because the original pointer is lost, and the allocated memory cannot be freed.

 

  • After that, `assignval()` is called again to assign the value 10 to the newly allocated memory. However, the memory allocation for the second `x` is unnecessary, and it results in a memory leak.

 

  • Finally, the code prints the value pointed to by `x` (the second `x` allocated inside the `if` statement), which may contain garbage data or an uninitialized value since it was not assigned a value.

 

  • The code frees the memory pointed to by `x` at the end, but this only frees the memory allocated inside the second `if` statement, leaving the memory allocated at the beginning of the `main()` function (the first `x`) leaked.

 

Therefore, the code compiles successfully but execution may result in a memory leak because the first `x` allocated memory is not freed before allocating memory for the second `x`.

Answer:

Related questions

68 votes
68 votes
5 answers
2