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

Returning pointers from functions

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>,
wonderboy <pa********@gmail.com> wrote:
: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.]
--
"I want to make sure [a user] can't get through ... an online
experience without hitting a Microsoft ad"
-- Steve Ballmer [Microsoft Chief Executive]
"I want to make sure [a user] can't get through ... an online
Nov 14 '05 #2

P: n/a
"wonderboy" <pa********@gmail.com> wrote:
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
Nov 14 '05 #3

P: n/a
wonderboy wrote:
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
Nov 14 '05 #4

P: n/a
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
Nov 14 '05 #5

P: n/a
In article <38*************@individual.net>,
Christian Kandeler <ch****************@hob.de_invalid> wrote:
: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.

: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. :(
--
Suppose there was a test you could take that would report whether
you had Free Will or were Pre-Destined. Would you take the test?
Nov 14 '05 #6

P: n/a
Walter Roberson wrote:
Christian Kandeler <ch****************@hob.de_invalid> wrote:
.... 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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7

P: n/a
On 2 Mar 2005 11:14:01 GMT, ro******@ibd.nrc-cnrc.gc.ca (Walter
Roberson) wrote:
In article <38*************@individual.net>,
Christian Kandeler <ch****************@hob.de_invalid> wrote:
: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.

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

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.