473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Output

#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
6 1828
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
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

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

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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
3
by: MatsL | last post by:
Hi, This is seriously driving me crazy, could anyone explain to me why neither of these doesn't produce XHTML compliant output (it is being called in Render() btw): output.WriteLine("<img...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.