C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Regarding "returning locally declared variable"

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
jim
Guest





PostPosted: Wed Oct 12, 2005 8:52 am    Post subject: Regarding "returning locally declared variable" Reply with quote



The following piece was copied from thinking in c++.
The qestion I have is : Since upper is local declared variable, will it
disappear
when return from the upperCase function? if so, is it a bug to write
the function this way?

inline string upperCase(const string& s) {
string upper(s);
for(size_t i = 0; i < s.length(); ++i)
upper[i] = toupper(upper[i]);
return upper;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
hydrajak@yahoo.com
Guest





PostPosted: Wed Oct 12, 2005 4:30 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote




jim wrote:
Quote:
The following piece was copied from thinking in c++.
The qestion I have is : Since upper is local declared variable, will it
disappear
when return from the upperCase function? if so, is it a bug to write
the function this way?

inline string upperCase(const string& s) {
string upper(s);
for(size_t i = 0; i < s.length(); ++i)
upper[i] = toupper(upper[i]);
return upper;
}



The answer is found in the section of yuor textbook that discusses
return by value and return by reference.

Good luck on the rest of your homework.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
ben
Guest





PostPosted: Wed Oct 12, 2005 4:32 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote



jim wrote:
Quote:
The following piece was copied from thinking in c++.
The qestion I have is : Since upper is local declared variable, will it
disappear
when return from the upperCase function? if so, is it a bug to write
the function this way?

inline string upperCase(const string& s) {
string upper(s);
for(size_t i = 0; i < s.length(); ++i)
upper[i] = toupper(upper[i]);
return upper;
}

The real function modified by the compiler behind your back may look
like this:

void upperCase(const string& s, string& _result)
{
string upper(s); // construct upper;
for (size_t i = 0; i < s.length(); ++i)
upper[i] = toupper(upper[i]);

_result.(string::string)(upper); // copy costruct
upper.~string(); // destroy upper
}

And the modified behind your back invocation may look like:

string s; // without calling constructor
upperCase(somestring, s);

So far the answer to your question is yes--upper gets destroyed after
its value copied. But in most cases where the Named Return Value
optimization is enabled, the modified behind your back function actually
looks more like this:

void upperCase(const string& s, string& _result)
{
for (size_t i = 0; i < s.length(); ++i)
_result[i] = _result(upper[i]);
}

In this case, upper is simply recognized as the return value and is thus
substituted. In this case your question cannot be answered because upper
does not exist--it is only a placeholder in source code level--never
constructed, never destroyed.

Regards,
Ben

P.S. Try to use std::transform for the above operation when you can.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
CodeCracker
Guest





PostPosted: Wed Oct 12, 2005 4:33 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote

Ofcourse upper will disappear but what you will get is the copy of the
variable and not the original variable so this code is not wrong. If
you would have returned the reference of the variable then it is very
dangerous. You don't know when the system overwrite the address refered
by upper.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
AnonMail2005@gmail.com
Guest





PostPosted: Wed Oct 12, 2005 4:34 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote

With respect to returning upper, there is no bug. You are returning
a string by value from the function so the return value is _copied_
to the resultant destination.

It's a bug if you would have returned a reference or pointer to a
locally declared variable since after you exit the function the
variable no longer exists.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Wed Oct 12, 2005 4:34 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote

In article <1129096206.739488.128130 (AT) f14g2000cwb (DOT) googlegroups.com>, jim
<jseers (AT) gmail (DOT) com> wrote:

Quote:
The following piece was copied from thinking in c++.
The qestion I have is : Since upper is local declared variable, will it
disappear
when return from the upperCase function? if so, is it a bug to write
the function this way?

inline string upperCase(const string& s) {
string upper(s);
for(size_t i = 0; i < s.length(); ++i)
upper[i] = toupper(upper[i]);
return upper;
}

this function returns a COPY of upper, not upper itself.

upper is destructed when it goes out of scope with the return,
the copy does not. [it is a compiler temporary which is
destructed,after its last use],

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
multimarine69@hotmail.com
Guest





PostPosted: Wed Oct 12, 2005 4:35 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote

Yes, the varible upper will disappear. But before it does you return
it's value and that's how you write functions. It's no bug it's exactly
what you are supposed to do.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Maeder
Guest





PostPosted: Wed Oct 12, 2005 4:37 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote

"jim" <jseers (AT) gmail (DOT) com> writes [rearranged by tm]:

Quote:
inline string upperCase(const string& s) {
string upper(s);
for(size_t i = 0; i < s.length(); ++i)
upper[i] = toupper(upper[i]);

First a side note:

This code looks dangerous. toupper() has undefined behavior unless its
argument is equal to EOF or has an unsigned char value.


Quote:
return upper;
}

Since upper is local declared variable, will it disappear when
return from the upperCase function?

Yes.


Quote:
if so, is it a bug to write the function this way?

No. A different, unnamed object is created as a copy of upper just
before upper is destructed; if the caller accesses the return value,
he will access that object.


[The compiler is entitled to optimize away the creation of the unnamed
object if the behavior of the program isn't altered apart from
copy-constructor and destructor calls by this optimization.]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Oct 12, 2005 4:38 pm    Post subject: Re: Regarding "returning locally declared variable" Reply with quote

jim wrote:
Quote:
The following piece was copied from thinking in c++.

inline string upperCase(const string& s) {
string upper(s);
for(size_t i = 0; i < s.length(); ++i)
upper[i] = toupper(upper[i]);
return upper;
}

The qestion I have is : Since upper is local declared variable, will it
disappear when return from the upperCase function?

Yes, it's scope is limited to the body of the function.

Quote:
if so, is it a bug to write the function this way?

No, the local variable is copied into the returnvalue before it is
destroyed.

Uli



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.