The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+23 votes

Consider the following three C functions:


int *g(void)
    int x = 10;
    return (&x);


int *g(void)
    int *px;
    *px = 10;
    return px;


int *g(void)
    int *px;
    px = (int*) malloc (sizeof(int));
    *px = 10;
    return px;

Which of the above three functions are likely to cause problems with pointers?

  1. Only $P3$
  2. Only $P1$ and $P3$
  3. Only $P1$ and $P2$
  4. $P1, P2$ and $P3$
asked in Programming by Veteran (68.8k points)
edited ago by | 2.4k views
P3 also fails when all malloc return NULL pointer right ??

2 Answers

+42 votes
Best answer

$[P1]$ may cause an error because function is returning the address of locally declared variable.

$[P2]$ will cause a problem because px is an int pointer that is not assigned with any address and we are doing dereferencing.

$[P3]$ will work because memory in bytes of size of int will be reserved and its address will be stored in px that can be further use, once function execution completes, this m/m will still exist in Heap until we free it using $free()$ function.

hence answer is (C)

answered by Veteran (38.4k points)
edited ago by
Yes, we can return. But we shouldn't use that address and access its contents as that can lead to undefined behaviour in C- process can crash or give garbage value.
It is supposed to return address of int, and it is doing the same. So, why type casting needed?
P3: isnot that a memory leak problem how come u will be ensure that it will work fine ?

It is not necessary P3 should cause memory leak. This can be handled by the caller as follows.

int *g(void)
    int *px;
    px = (int*) malloc (sizeof(int));
    *px = 10;
    return px;
int main()
        int *p = g();
        printf("%d\n", *p);


P3 also has problem. what if malloc returns null then? you are accessing *px, which gives runtime error.
Again "likely" is there in question :)
Where can we read more about this kind of function behaviour and examples??
This is about knowing the properties of variables- like scope and lifetime. No other concept involved. For questions, may be you can take GO Programming test, but previous GATE ones are also enough.
@arjun sir. whether p1 will always throw an error or not. I understand x is a local variable and its scope is limited to the lifetime of function g. once the function g is executed variable x will be no longer available in the memory. sir, i tried to execute the same type of program on codechef compiler but I didn't get any error. plz correct me where am i wrong

#include <stdio.h>
int *call(void)
    int x=10;
int main(void) {
    int *p;
    return 0;
Please explain why P1(returning address of locally declared variable) will cause error, since the return type match is not violated.
if u write a c program of p1 and use an extra function in main section then it has the maximum probabilty of showing garbage values rather than value 10 ...
dhanyawad mam
g is returning address of x , and address are hexadecimal ..but the return type of g is int. Is this the reason of showing error ? Am I correct ?
+4 votes
p1  will cause problem as x is a local variable and the return address will be returned the value of x which will be null after function ends.

p2 will cause problem as the pointer is not initialised.

p3 will cause error as if no memory is allocated by the OS the malloc function will return Null and it will be dangling pointer problem.


so right answer is D.
answered by (59 points)

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,619 questions
39,267 answers
36,653 users