[Solved] .. warning: taking address of temporary



  • Hi

    @char ..Protocol(QByteArray *data)
    {....
    if(!VerifyCS(&data->mid(1,4), data->at(6)))
    ... }
    @

    returns warning: taking address of temporary upon first compile only for line 3 of the code snippet above.
    I am not really sure what it wants to tell me and the warning disappears on subsequent builds.
    Any hints?

    Thanks,
    McL


  • Moderators

    @
    &data->mid(1,4)
    @

    is the address of a temporary return value, which exists only within those parenthesis. The warning tells you that this is potentially dangerous. Therefore the warning.
    The warning shall appear every time you compile this file. The file compiled when you do a rebuild too, but when you simply build and the file is not changed, it will not be compiled. Hence you will not see the warning.



  • i believe that it refers to the first argument of your "VerifyCS" function:

    @data->mid(1,4)@

    it returns QByteArray by value, which has no named reference from the point in the scope where it's called from (in the argument list), thus being a temporary.

    this is easy to misuse, especially in relation to the (probable) pointer usage
    i'd suggest passing reference-by-const,
    but if you really need to modify the 1st argument in "VerifyCS", try something like

    @
    QByteArray& header = data->mid(1, 4);

    if(!VerifyCS(header, data->at(6))

    // or, if you don't want to alter your function prototype
    // in order to use references

    if(!VerifyCS(&header, data->at(6))
    @



  • You guys are right, the problem seems to be the mid.
    However, I don't really get the warning. To me, this seems a normal use of pointer.
    Main gets data ... forward by reference to protocol-checker ... which calls a CS checker by reference due to multiple CS's at different places in the data which can be very large as well. The data does not need to be modified in any place so far, just structure and CS checking, and it doesn't make much sense to copy by value.

    @if(!VerifyCS(&data->mid(1,4), 4, data->at(5))) // check CS of size
    ...
    if(!VerifyCS(&data->right(uDataSize.l+1), uDataSize.l, data->at(data->size()-1)))// check CS of data@


  • Moderators

    Just imagine this
    @
    QByteArray *ptr = 0;
    if ( true )
    {
    ptr = &data->mid(1,4);
    }
    cout << ptr->at ( 0 );
    @

    Most likely this code will crash. Why? Because you have taken the address of temporary object. The compile will warn you there might be a problem. However, it will not give an error message even though, this is probably crashing.

    Typically, the situations are a bit more complex not so easy to comprehend. If there a couple of more lines around you will have a hard time to find the problem.

    In your case it might not be a problem, but the compiler does not know exactly. Therefore, it is giving you a heads up. In case you would store the pointer internally in the function you are calling, it is save for the time being, but not necessarily later on, when you would try to access.

    A warning is a warning. It is your decision to watch or not.


  • Lifetime Qt Champion

    To add to koahnig (and after being bitten by that while debugging someone else's code)

    A warning is a potential crash in your app at the worst moment (generally at a demo time), thus I would recommend to fix them when possible.



  • OK, convinced. Changed it to:
    @ ...
    QByteArray size = inData->mid(1, 4);
    if(!VerifyCS(&size, 4, inData->at(5)))
    ...
    QByteArray data = inData->mid(6, uDataSize.l);
    if(!VerifyCS(&data, uDataSize.l, inData->at(inData->size()-1)))
    ...
    @
    and the compiler is happy now ... and I am safe :-)
    Thank you guys!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.