By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

Output

P: n/a
#include<stdio.h>
int *f1()
{
int a=20;
return &a;
}
f2()
{
int b=10;
return b;
}
int main(void)
{
int a;
int *p;
p=f1();
f2();
a=*p;
printf("%d",a);
}

The output should be 20..am I right???
When I compiled the program in Turbo C/C++ ....the output is 10 i.e b
value....How is that possible?

Please help.

Dec 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
raghu wrote:
#include<stdio.h>
int *f1()
{
int a=20;
return &a;
Bang! You have just returned the address of a local variable, take
care, your toilet my explode.
}
f2()
{
int b=10;
return b;
}
int main(void)
{
int a;
int *p;
p=f1();
f2();
a=*p;
printf("%d",a);
}

The output should be 20..am I right???
Wrong, the output is undefined.
When I compiled the program in Turbo C/C++ ....the output is 10 i.e b
value....How is that possible?
Anything is possible with undefined behaviour.

--
Ian Collins.
Dec 21 '06 #2

P: n/a
raghu wrote:
#include<stdio.h>
int *f1()
{
int a=20;
return &a;
a is an automatic variable and its life ends with the return from the
function. Returning the address of something that need not exist on
return is a very bad idea. Either declare a to be an int * and malloc
the space or declare a to be static.
}
f2()
{
int b=10;
return b;
}
int main(void)
{
int a;
int *p;
p=f1();
f2();
a=*p;
printf("%d",a);
}

The output should be 20..am I right???
No, you are not.
When I compiled the program in Turbo C/C++ ....the output is 10 i.e b
value....How is that possible?
It could be anything, including a failing illegal access.
Dec 21 '06 #3

P: n/a

raghu wrote:
#include<stdio.h>
int *f1()
{
int a=20;
return &a;
}
f2()
{
int b=10;
return b;
}
int main(void)
{
int a;
int *p;
p=f1();
f2();
a=*p;
printf("%d",a);
}

The output should be 20..am I right???
When I compiled the program in Turbo C/C++ ....the output is 10 i.e b
value....How is that possible?

Please help.

Hello,
You are trying to return the address of local variable from
function f1. The memory allocated to variable 'a' in f1() gets
deallocated when the function exits. The value stays there until f2()
is called which overwrites it causing value of variable 'b' to be
displayed.

Dec 21 '06 #4

P: n/a

Computer Wizard wrote:
raghu wrote:
#include<stdio.h>
int *f1()
{
int a=20;
return &a;
}
f2()
{
int b=10;
return b;
}
int main(void)
{
int a;
int *p;
p=f1();
f2();
a=*p;
printf("%d",a);
}

The output should be 20..am I right???
When I compiled the program in Turbo C/C++ ....the output is 10 i.e b
value....How is that possible?

Please help.


Hello,
You are trying to return the address of local variable from
function f1.
True and that was his problem, a genuine generic problem. The gcc
compiler on my linux system gave a warning about this.
The memory allocated to variable 'a' in f1() gets
deallocated when the function exits. The value stays there until f2()
is called which overwrites it causing value of variable 'b' to be
displayed.
True, in _this_ case, but not necessarily true every where...

Another platform/compiler/optimisation-level combination may have
decided that as "int b" in f2() was unused, the compiled code could
return the literal value 10 by putting in a register, for example.

On a system running AIX, the Xlc compiler (with default optimisation
levels) generated an executable which displayed 0. I neither know nor
care why... (I could find out if I wanted/needed to)

Dec 21 '06 #5

P: n/a
GKB
Hi Raghu,

You are trying to return a local variable address here. What happens in the
cpu when a function is called ? The life time of the local variable is only
within the function, why ?

Hope you know about the PC(program counter) and SP(stack pointer) if not a
PC is a cpu register where the address of the next instruction to be
executed is stored. SP is a cpu register holds the address of top of the
Stack.

When a function is called the PC value has to be altered on the fly and it
has to execute the instructions of the subroutine(or funtion) and once it is
done it has to 'return' back to the calling routine and execute instruction
from where is left.

The stack comes to picture here, when function is called a stack frame is
created and the return address, the function arguments are variables are
'Pushed' on to the stack and now it changes the PC register to the
subroutine's instruction address and start executing the subroutine. Once
it is done it will 'return' by Poping out the return address from the stack
and when doing this the SP is decremented. The contents in stack will be
still there until when some other function is called and alters the value.
In your case that is the reason you see 10, which is expected with the above
theory.

Geeks,

I am open for discussion on this topic...

Thanks,
-Kumar.
"raghu" <ra*********@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
#include<stdio.h>
int *f1()
{
int a=20;
return &a;
}
f2()
{
int b=10;
return b;
}
int main(void)
{
int a;
int *p;
p=f1();
f2();
a=*p;
printf("%d",a);
}

The output should be 20..am I right???
When I compiled the program in Turbo C/C++ ....the output is 10 i.e b
value....How is that possible?

Please help.

Dec 21 '06 #6

P: n/a

GKB wrote:
Hi Raghu,
1) The (rather strong) preference in this news group is not to
top-post. If you're replying put your reply after the material you're
replying to..
You are trying to return a local variable address here. What happens in the
cpu when a function is called ? The life time of the local variable is only
within the function, why ?
Not for the reasons you outline...
Hope you know about the PC(program counter) and SP(stack pointer) if not a
PC is a cpu register where the address of the next instruction to be
executed is stored. SP is a cpu register holds the address of top of the
Stack.
Really? Always? On every machine that has a compliant C implementation?
(Hint: This is a rhetorical question)

[Snip]
>
Geeks,
Who/what/where are they?
I am open for discussion on this topic...
I hope you're open for education on this topic. Please read and
meditate on http://www.lysator.liu.se/c/ten-commandments.html
especially commandment 10. The world is much bigger and more
interesting than you know, and not every C implementation has the same
internals that you currently know about.

Dec 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.