Returning pointers from functions

W

wonderboy

Hey guys,
I have a simple question. Suppose we have the following
functions:-

//-----My code starts here

char* f1(char* s)
{
char* temp="Hi";
return temp;
}

char* f2(char *s)
{
char temp[100];
strcpy(temp,"Hi");
strcat(temp,s);
return temp;
}

//-----My code ends here


Now, suppose I have a main function which calls the both. Thing is,
when i compile the prog, the compiler gives me a warning regarding f2
saying that temp is a local variable and i m returning its address.

str1.cpp: In function `char* f2(char*)':
str1.cpp:14: warning: address of local variable `temp' returned

This is fine and expected. But y dont i get the same warning for f1? At
runtime, everything works fine although i guess this is not right as
the memory for the local variables can get overwritten anytime. Pls,
can someone clarify the whole concept. TIA.

Cheers

PS- if this has been discussed already, pls provide the link and my
apologies in dat case.
 
W

Walter Roberson

:char* f1(char* s)
:{
: char* temp="Hi";
: return temp;
:}

:when i compile the prog, the compiler gives me a warning regarding f2
:saying that temp is a local variable and i m returning its address.

:This is fine and expected. But y dont i get the same warning for f1?

In f1, you are not returning the -address- of temp, you are returning
the -value- of temp, which happens to be a pointer to a character
array. Compilers often put quoted strings into static storage, as
that saves having to copy in the string each time. It is legal
for compilers to use the -same- location for all instances of
the same quoted string, and it legal for compilers to put quoted
strings into read-only memory.

None the less, -logically- the string could be anywhere
(including on the stack) and so you should not be returning
it's address. Well, you can return it's address, but you
shouldn't -use- that address for anything (other than
as a curiosity) as there is no certainty at all that the
storage will continue to persist, or even that the pointer
will still be usable

[Pointers don't have to have absolute references: it would be legal for
an implimentation to work with a system of "public" and "private"
pointers, where a "public" pointer was an absolute address in virtual
memory but a private pointer was a relative offset from a base register
whose value changed from function to function. It is only legal
to compare pointers with pointers when they are pointers into the
same object.]
 
R

Richard Bos

wonderboy said:
char* f1(char* s)
{
char* temp="Hi";
return temp;
}

char* f2(char *s)
{
char temp[100];
strcpy(temp,"Hi");
strcat(temp,s);
return temp;
}
str1.cpp: In function `char* f2(char*)':
str1.cpp:14: warning: address of local variable `temp' returned

Be careful! C++ is not C, nor a superset of it. There _are_ things that
function differently in C++ than in C, and I do not know whether this is
one of them.
This is fine and expected. But y dont i get the same warning for f1?

Because a string literal has static duration. This means that it is
created sometime before main() is called, and destroyed just before your
program exits. It is always available while your program runs. In f1 you
aren't returning a pointer to a local variable; you are returning the
value of a local variable, and that value is a pointer to a static
object. It doesn't matter, in this case, that temp will be destroyed
when f1 returns, because the array it points at will not.

Richard
 
C

Christian Kandeler

wonderboy said:
char* f1(char* s)
{
char* temp="Hi";
return temp;
}

This is okay. You return a copy of the value of temp, which is the address
of the string literal "Hi". The string literal is an array of static
storage duration, which means that it is available throughout the entire
lifetime of the program.
char* f2(char *s)
{
char temp[100];
strcpy(temp,"Hi");
strcat(temp,s);
return temp;
}

This is not okay. You return the address of the first element of temp, which
is an object local to f2. Therefore, references to it become invalid after
exit from the function.


Christian
 
C

Christian Kandeler

Walter Roberson wrote:

[ about string literals ]
None the less, -logically- the string could be anywhere
(including on the stack) and so you should not be returning
it's address. Well, you can return it's address, but you
shouldn't -use- that address for anything (other than
as a curiosity) as there is no certainty at all that the
storage will continue to persist, or even that the pointer
will still be usable

This is wrong. The standard mandates that string literals have static
storage duration (6.4.5 ), which means that "[t]he object exists, has a
constant address, and retains its last-stored value throughout the
execution of the entire program" (6.2.4).


Christian
 
W

Walter Roberson

:[ about string literals ]
:> None the less, -logically- the string could be anywhere
:> (including on the stack) and so you should not be returning
:> it's address.

:This is wrong. The standard mandates that string literals have static
:storage duration

I gotta look into getting the standard on CD. My paper copy is
always somewhere else, and would fall apart even more if I lugged
it around. :(
 
C

CBFalconer

Walter said:
.... snip ...

: This is wrong. The standard mandates that string literals have
: static storage duration

I gotta look into getting the standard on CD. My paper copy is
always somewhere else, and would fall apart even more if I lugged
it around. :(

You can google for N869, or get the just mounted slightly edited
text version (suitable for searching and quoting) at:

<http://cbfalconer.home.att.net/download/n869_txt.bz2>

Please fix your abnormal quote char, which fouls up software meant
to handle usenet quotations.
 
A

Alan Balmer

:[ about string literals ]
:> None the less, -logically- the string could be anywhere
:> (including on the stack) and so you should not be returning
:> it's address.

:This is wrong. The standard mandates that string literals have static
:storage duration

I gotta look into getting the standard on CD. My paper copy is
always somewhere else, and would fall apart even more if I lugged
it around. :(

ANSI will sell you a PDF version for $18.00.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top