Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here are two problems what are their output and why?
C++
#include<stdio.h>                      #include<stdio.h>
#include<conio.h>                      #include<conio.h>
int *call()                           int *call()
{ int x=5;                             { int x=5;
  ++x;                                   ++x;
  return &x;                             return &x;
}                                      }
void main()                             void main()
{ int *ptr;                             { clrscr();
                                         int *ptr;
  ptr=call();                            ptr=call();
  clrscr();                             
  printf("value:%d",*ptr);                printf("value:%d",*ptr);
}                                       }
Posted
Updated 16-Jun-11 23:14pm
v5
Comments
Christian Graus 17-Jun-11 4:46am    
Why did you ask this ? It smells like homework to me.

They are practically identical and both broken (you can't even compile them).
In function class you are returning a pointer to a temporary variable (x will be destroyed some time after the function returns).
in main function you are calling the not existing function 'call'.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Jun-11 4:48am    
Sure, a 5.
--SA
Albert Holguin 17-Jun-11 9:10am    
yep.. 5
Carlos is right: that is a very bad idea.
If you fix the compilation problems, and actually call the routine, there is a very good chancve of either randon output, or you program blowing up in the near future.
This is because by default C and C++ allocate local variabals on the stack - which is a single memory space shared by all routines. When you return a pointer to a local variable, you are returning a pointer to an area of memory which will be re-used by other routines when they are called - hence why your two programs produce different outputs. In the example on teh left, theve memory used by the local variable that ptr points at is overwritten by the clrscr routine.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Jun-11 4:48am    
My 5.
--SA
CPallini 17-Jun-11 9:17am    
Carlos? :-D
OriginalGriff 17-Jun-11 9:22am    
Sorry! Probably brain death on my part. :O
1:shows garbage value eg: -10.
2:shows correct output '6' and incremented value for other inputs.
why correct in second option?
can you explain it why clrscr() give different outputs in this question or respect to routines?
 
Share this answer
 
Comments
Albert Holguin 17-Jun-11 9:14am    
if either option works, its merely coincidence and not helped by your programming. why coincidence, the stack is probably not being cleared by the time you make the second call (when it works) but its mere coincidence. no function or method should EVER return a pointer to a variable on its local stack, since it gets deallocated at the end of the function call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900