开发者

returning address or local variable or temporary C++ warning [duplicate]

开发者 https://www.devze.com 2023-01-19 08:33 出处:网络
This question already exists: Closed 12 years ago. Possible Duplicate: c++ warning: address of local variable
This question already exists: Closed 12 years ago.

Possible Duplicate:

c++ warning: address of local variable

Hi, When i write this code:

//Returns the transpose matrix of this one
SparseMatrix& SparseMatrix::transpose()const{
    vector<Element> result;
    size_t i;
    for(i=0;i<_matrix.size();++i){
        result.push_back(Element(_matrix.at(i)._col, _matrix.at(i)._row, _matrix.at(i)._val));
    }

    return SparseMatrix(numCol,numRow,result);
}

I get the warning "returning address or local variabl开发者_Go百科e or temporary". The last line calls the SparseMatrix constructor. I don't understand what's wrong with this code, and how can i fix it so i can return a SparseMatrix object as i want.


You're returning a reference, not an actual object - note the & here:

SparseMatrix& SparseMatrix::transpose()const{

If you want to return the actual object, remove that &.

The last line does indeed call the constructor, but it doesn't return the resulting object. That object immediately gets destroyed, and an invalid reference to it gets returned.


In C++, local variables are 'automatically' destructed when going out of scope. Your return statement will create a nameless, temporary variable of type SparseMatrix that will immediately go out of scope. Hence, returning a reference to it doesn't make sense.

It may be easier to return by value: then a copy of the temporary will be returned. The compiler can optimize that away (copy elision).

If you really want to pass an object out of a function, you should create it on the heap, using new:

SparseMatrix* SparseMartix::transopse()const{


     //...
     return new SparseMatrix(...);

}

But then, you need to take care of the lifetime of the returned object yourself.


The construct 'T()' creates a temporary of type 'T' which is basically not an Lvalue (but an Rvalue).

$12.1/11 - "A functional notation type conversion (5.2.3) can be used to create new objects of its type. [ Note: The syntax looks like an explicit call of the constructor.

12 An object created in this way is unnamed. [ Note: 12.2 describes the lifetime of >temporary objects. —end note ] [ Note: explicit constructor calls do not yield lvalues, see 3.10. —end note ] The lifetime of this temporary is the end of the full expression i.e. the ending semicolon after the expression.

$12.2/3 - "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception. The value computations and side effects of destroying a temporary object are associated only with the full-expression, not with any specific subexpression."

$12.2/5- 'The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not extended; the temporary is destroyed at the end of the full-expression in the return statement."

Therefore your function tries to return a reference to a memory location whose storage duration is already over and the object has been destroyed.

Therefore a warning. Note that this situation is not required to be explicitly diagnosed by the Standard and hence a warning.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号