473,385 Members | 1,400 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.

Can a function return statement be elided?

I have a resource handle class (call it ResourceHandle) that works
like std::auto_ptr to automatically manage resource pointers from a C
API (call them T*):

class ResourceHandle
{
public :
ResourceHandle(T* ptr) { /* take ownership of T* */ }
~ResourceHandle() { /* release T* resource */ }
};

ResourceHandle f() { /* return a new T* */ }

Function f() relies on ResourceHandle(T*) being called as a result of
the return statement to avoid a resource leak. But what happens if
the return value of f() is not used in the calling function?

int main()
{
ResourceHandle resource(f()); // no leak
f(); // resource leak if return statement is elided?
}
Jul 22 '05 #1
2 1370
"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google.c om...
class ResourceHandle
{
public :
ResourceHandle(T* ptr) { /* take ownership of T* */ }
~ResourceHandle() { /* release T* resource */ }
};

ResourceHandle f() { /* return a new T* */ }

Function f() relies on ResourceHandle(T*) being called as a result of
the return statement to avoid a resource leak. But what happens if
the return value of f() is not used in the calling function?

int main()
{
ResourceHandle resource(f()); // no leak
f(); // resource leak if return statement is elided?
}


Even if the result of evaluating f() is not used in any other way, it will
still be destroyed.

However, your program may have a serious problem. Your class ResourceHandle
does not have a copy constructor, which means that when you copy a
ResourceHandle object, doing so implicitly copies its members. Now consider
this:

ResourceHandle f()
{
ResourceHandle res;

// Do something here that gives a value to result

return res;
}

When f exits, res will be copied to serve as the return value and then
destroyed. Whatever resource res refers to will presumably be freed. Now,
on the caller's side:

f();

f returns a result that is put in a compiler-generated temporary and then
destroyed. That result is a copy of the local variable named in function f.
When the result is destroyed, the resource to which it refers is
freed--presumably for the second time.

I suspect that behavior is not what you have in mind.
Jul 22 '05 #2
"Andrew Koenig" <ar*@acm.org> wrote in message news:<mM********************@bgtnsc04-news.ops.worldnet.att.net>...
Even if the result of evaluating f() is not used in any other way, it will
still be destroyed.

However, your program may have a serious problem. Your class ResourceHandle
does not have a copy constructor, which means that when you copy a
ResourceHandle object, doing so implicitly copies its members. Now consider
this:


I just posted sample code to help illustrate my question. The real
version of ResourceHandle is implemented like std::auto_ptr. If I use
an auto_ptr example I think my question will be more clear.

auto_ptr<T> f()
{
return new T;
}

Will the auto_ptr constructor be called in this case if the return
value is ignored? Or is it safer to write the function like this to
ensure that the return value gets constructed (and thus destructed):

auto_ptr<T> f()
{
return auto_ptr<T>(new T);
}
Jul 22 '05 #3

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

Similar topics

4
by: bluedolphin | last post by:
This is my first function in Visual and I'm having a simple syntax syntax issue that I'm hoping someone can help correct for me. I have a function Public Function...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
13
by: jimjim | last post by:
Hello all, I ve come across the following code fragment and I was wondering why is the copy ctr called on return (rather than just returning the string statement obj. TIA string...
8
by: Ian Mackenzie | last post by:
Hi Guys I am VERY new to DB2 and have created a workingdays function to return the working days between 2 dates, but I get some compiler errors when running it: CREATE FUNCTION WORKINGDAYS...
3
by: mark.bergman | last post by:
Running lint on code initialising a local 2-D array gives a warning. void f(void) { int a = { 0 }; ... lint gives "warning: Partially elided initialisation..." Should this be happening,...
4
by: Jeroen | last post by:
Hi, Assume a function which returns an object of type 'my_class': my_class get_class() { ... } Now I have the following question:
9
by: Rob | last post by:
I want to do something like this: template <typename MyType> std::vector<MYTypeget(SomeClass c) { ...elided... } int main()
4
by: Rob | last post by:
If I have a function that has a variable declared inside it (which is a pointer) that can be set equal to a pointer returned from another function, do i need to delete it before returning from the...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.