Re: Who could tell me the difference between two code?
- From: "Vipin [MVP]" <Vipin@xxxxxxxxxx>
- Date: Thu, 16 Feb 2006 13:30:36 +0530
tmp is on the stack and its scope is limited to the function,
once the function returns, the stack pointer moves, stack frame released
and the memory to which tmp initially referred to, is available for reuse
for other local variables and for possible use in stack frames for future
function calls.
Another way of doing this is to do like this:-
int* fn()
{
static int tmp=123;
return &tmp;
}
In this case although the name has a local scope the memory corresponding
to the variable tmp is available thoughout the lifetime of the program.
Another way is like this:-
int* fn()
{
int *tmp= new int;
*tmp = 123;
return tmp;
}
--
Vipin Aravind
"Lee Tow" <fbjlt@xxxxxxxxxxxxx> wrote in message
news:e8v3UnsMGHA.1616@xxxxxxxxxxxxxxxxxxxxxxx
Hello all:
Look:
int* fn()
{
int tmp=123;
return &tmp;
}
void main()
{
cout<<*fn()<<endl;
}
and when I compile the code,it display a warn:
returning address of local variable or temporary.
and look other code:
char* fn()
{
char *tmp="Hello";
return tmp;
}
void main()
{
cout<<fn()<<endl;
}
but it don't display a warn when I compile this code,
the both codes are very similar,but why do they display
different result?thank you.
.
- Follow-Ups:
- Re: Who could tell me the difference between two code?
- From: Joseph M . Newcomer
- Re: Who could tell me the difference between two code?
- References:
- Who could tell me the difference between two code?
- From: Lee Tow
- Who could tell me the difference between two code?
- Prev by Date: Re: Changing dialog look-n-feel background color
- Next by Date: Newbie-Pls help me with DataBase Connection in EVC++.
- Previous by thread: Re: Who could tell me the difference between two code?
- Next by thread: Re: Who could tell me the difference between two code?
- Index(es):
Relevant Pages
|