473,385 Members | 1,940 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,385 software developers and data experts.

Function argument lifespan

All,

As the simple sample program below demonstrates, function arguments are
destroyed after the return value of the function has been evaluated. As
opposed to local function variables, which are destroyed before the
function returns.

Is this correct? (To the best of my knowledge it is)
Where in the standard is this described?

Thanks,
Andre

--- Example ---

#include <iostream>

using namespace std;

class test
{
public:
test( string s = "")
: s_(s)
{
cout << "test::test()\t" << s_ << endl;
}

test( const test &org)
{
cout << "test::test(copy)" << endl;
s_ = "copy of " + org.s_ ;
}

~test()
{
cout << "test::~test()\t" << s_ << endl;
}

const string & getStr() const
{
return s_;
};

private:
string s_;
};

const test & f( test arg )
{
test l("local");
return arg;
}

int main()
{
test c("main");
cout << "\n\t\tf() = [" << f( c ).getStr() << "]\n" << endl;
}

Oct 19 '05 #1
10 2008
in*****@gmail.com wrote:
As the simple sample program below demonstrates, function arguments
are destroyed after the return value of the function has been
evaluated. As opposed to local function variables, which are
destroyed before the function returns.

Is this correct? (To the best of my knowledge it is)
Where in the standard is this described?


3.8 Object lifetime, 12.2 Temporary objects. Probably more...

V
Oct 19 '05 #2

Victor Bazarov wrote:
Where in the standard is this described?


3.8 Object lifetime, 12.2 Temporary objects. Probably more...

V


Victor,

Thank you very much for your quick and knowledgable answer!
Much appreciated!

Thanks,
Andre

Oct 19 '05 #3

in*****@gmail.com wrote:
All,

As the simple sample program below demonstrates, function arguments are
destroyed after the return value of the function has been evaluated. As
opposed to local function variables, which are destroyed before the
function returns.

Is this correct? (To the best of my knowledge it is)
Where in the standard is this described?

Thanks,
Andre

--- Example ---

#include <iostream>

using namespace std;

class test
{
public:
test( string s = "")
: s_(s)
{
cout << "test::test()\t" << s_ << endl;
}

test( const test &org)
{
cout << "test::test(copy)" << endl;
s_ = "copy of " + org.s_ ;
}

~test()
{
cout << "test::~test()\t" << s_ << endl;
}

const string & getStr() const
{
return s_;
};

private:
string s_;
};

const test & f( test arg )
{
test l("local");
return arg;
}
The f() routine returns a reference to a function parameter whose
lifetime ends at function exit. Therefore I believe that the behavior
of your program is undefined.
int main()
{
test c("main");
cout << "\n\t\tf() = [" << f( c ).getStr() << "]\n" << endl;
}


I don't believe it's possible to draw any sound conclusions from
running this program due to the implementation of f() above.

Greg

Oct 19 '05 #4
This is nonsense sample, cause f() returns a reference to a temporary
object which is copy of "test arg".

Oct 19 '05 #5
Greg wrote:
const test & f( test arg )
{
test l("local");
return arg;
}


The f() routine returns a reference to a function parameter whose
lifetime ends at function exit. Therefore I believe that the behavior
of your program is undefined.


Actually, I do think the behaviour is defined. My original question was
how to better define why it is legal - i.e. where it is mentionend in
the standard.

Victor pointed me to item 12.2 in the C++ Standard. I believe
paragraphs 2 and onward explain it (too long to reproduce here).

Given the example code (para. 2):
X f(X); // X is a class
...
X a(1);
a=f(a);

The standard writes:
"...the expression a=f(a) requires a temporary for either the
argument 'a' or the result of f(a) to avoid undesired aliasing of 'a'."

B.Stroustrup writes (similar wording to the standard para. 3) in "The
C++ Programming Language", section 10.4.10:
"Unless bound to a reference or used to initialize a named object, a
temporary object is destroyed at the end of the full expression in
which it was created."

IMHO the temporary object (arg) is created as part of the cout <<
f(c)... expression. Thus it exists until the end of that whole
expression (once again IMHO).

Is this not correct?

Cheers,
Andre

Oct 19 '05 #6
aling wrote:
This is nonsense sample,
What part is nonsense?
What is your definition of nonsene in this context?
cause f() returns a reference to a temporary object
That part I agree with...
which is copy of "test arg".


....this part I don't.

A reference to "arg" is returned, which is a copy of "c".
"arg" was created as part of the f(c) expression (IMHO).

Cheers,
Andre

Oct 19 '05 #7
"in*****@gmail.com" wrote:

IMHO the temporary object (arg) is created as part of the cout <<
f(c)... expression. Thus it exists until the end of that whole
expression (once again IMHO).


Actually I think you are right. *In this case* the whole thing
is defined.
Nevertheless IMHO it is poor programming praxis, if the validity
of a function depends on the exact details that function is used.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 19 '05 #8
in*****@gmail.com wrote:
Greg wrote:
const test & f( test arg )
{
test l("local");
return arg;
}


The f() routine returns a reference to a function parameter whose
lifetime ends at function exit. Therefore I believe that the behavior
of your program is undefined.


Actually, I do think the behaviour is defined. My original question was
how to better define why it is legal - i.e. where it is mentionend in
the standard.

Victor pointed me to item 12.2 in the C++ Standard. I believe
paragraphs 2 and onward explain it (too long to reproduce here).

Given the example code (para. 2):
X f(X); // X is a class
...
X a(1);
a=f(a);

The standard writes:
"...the expression a=f(a) requires a temporary for either the
argument 'a' or the result of f(a) to avoid undesired aliasing of 'a'."

B.Stroustrup writes (similar wording to the standard para. 3) in "The
C++ Programming Language", section 10.4.10:
"Unless bound to a reference or used to initialize a named object, a
temporary object is destroyed at the end of the full expression in
which it was created."

IMHO the temporary object (arg) is created as part of the cout <<
f(c)... expression. Thus it exists until the end of that whole
expression (once again IMHO).

Is this not correct?

Cheers,
Andre


You are correct abut the temporary. But the program's behavior is still
undefined.

The problem is not the lifetime of the temporary reference that the
function returns, but the lifetime of the object that it is bound to.
It is bound to a function parameter passed by value - not by reference.
Because it is a value parameter, its lifetime is certain to end before
the function exits. So the return value used in the calling routine is
in fact in scope, but the variable it is bound to has been destroyed.
Accessing a reference to a deallocated variable is undefined behavior.

Greg

Oct 19 '05 #9

Greg wrote:
in*****@gmail.com wrote:
Greg wrote:
.... The problem is not the lifetime of the temporary reference that the
function returns, but the lifetime of the object that it is bound to.
It is bound to a function parameter passed by value - not by reference.
Because it is a value parameter, its lifetime is certain to end before
the function exits. So the return value used in the calling routine is
in fact in scope, but the variable it is bound to has been destroyed.
Accessing a reference to a deallocated variable is undefined behavior.


I think that the bottom line of our disagreement is the question
whether a pass-by-value function argument has function scope or not. I
do not believe it does.

The function argument - especially in the case of a complex class - is
allocated (and copy constructed) BEFORE the function is entered. Thus
its lifetime is not within the function scope but bound by the complete
expression it is part of. As such, as outlined by the previous
quotations, I believe it is not destroyed before the full expression is
evaluated.

This case only really matter of course if you return a reference to the
argument like I did. But it can be illustrated similarly even with a
function that doesn't return the argument in any form:

int f1( my_class arg )
{
return 42;
}

When calling this function, a temporary my_class object is allocated
and copy constructed, and not destroyed until the full expression it
may be part of is evaluate.

E.x:
cout << f1( c ) << " is the answer." << endl;

That is how I understand and interpret the standard - I may be wrong.

Cheers,
Andre

Oct 19 '05 #10

in*****@gmail.com wrote:
Greg wrote:
in*****@gmail.com wrote:
Greg wrote:

...
The problem is not the lifetime of the temporary reference that the
function returns, but the lifetime of the object that it is bound to.
It is bound to a function parameter passed by value - not by reference.
Because it is a value parameter, its lifetime is certain to end before
the function exits. So the return value used in the calling routine is
in fact in scope, but the variable it is bound to has been destroyed.
Accessing a reference to a deallocated variable is undefined behavior.


I think that the bottom line of our disagreement is the question
whether a pass-by-value function argument has function scope or not. I
do not believe it does.


Sorry to reply to my own post...

I think I actually have to retract my last statement. Thinking about
this some more, and re-reading the function declaration section, I
believe I was actually wrong and Greg's assertions are correct in every
respect.

So thanks Greg for clarifying.

Cheers,
Andre

Oct 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
4
by: Nicholas Beenham | last post by:
Hi all, Is there a way to set the lifespan of a thread. I am trying to start a process that may or may not have an outcome and want to finish it after a certain length of time finished or not....
4
by: abCSharp | last post by:
I understand that static variables have app-domain scope. Till the app-domain is in memory, the static variable will be in memory. When are the app-domains unloaded is the question. I have read...
20
by: asdf | last post by:
True or false ?
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; When a Page object - the code-behind of an aspx page is created - what is the lifespan of that created object? Is it tied to the session, the request/response, or is it in a poll and reused...
3
by: David Cox | last post by:
I found some details on a musical festival, made my plans, and only found out by chance that the site was two years out of date. Is there some HTML way of setting a lifespan on a webpage on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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

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.