Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved] .. warning: taking address of temporary
QtWS25 Last Chance

[Solved] .. warning: taking address of temporary

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 4 Posters 11.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    McLion
    wrote on 20 Jan 2014, 12:34 last edited by
    #1

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 20 Jan 2014, 13:26 last edited by
      #2

      @
      &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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        compor
        wrote on 20 Jan 2014, 13:52 last edited by
        #3

        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))
        @

        purposes as understood by the purposer will be misunderstood by others

        1 Reply Last reply
        0
        • M Offline
          M Offline
          McLion
          wrote on 20 Jan 2014, 18:33 last edited by
          #4

          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@

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on 20 Jan 2014, 20:04 last edited by
            #5

            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.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Jan 2014, 20:29 last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                McLion
                wrote on 21 Jan 2014, 10:49 last edited by
                #7

                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!

                1 Reply Last reply
                0

                3/7

                20 Jan 2014, 13:52

                • Login

                • Login or register to search.
                3 out of 7
                • First post
                  3/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved