Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is QList out-of-scope when function returns?
QtWS25 Last Chance

Is QList out-of-scope when function returns?

Scheduled Pinned Locked Moved Solved General and Desktop
stackqt6out-of-scopecalculatelayout
5 Posts 3 Posters 485 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.
  • E Offline
    E Offline
    EricR
    wrote on 4 Apr 2024, 12:45 last edited by
    #1

    While debugging a problem in my Qt application, I came across the following snippet of code (which I have shrunk for the purpose of this question) that I don't quite understand. The snippet is from the Qt 6.6.1 library source code.

    QList<qreal> ChartValueAxisY::calculateLayout() const
    {
        QList<qreal> points;
        for (int i = 0; i < 10; ++i)
        {
            points[i] = qreal(i);
        }
        return points;
    }
    

    My understanding is that the QList of qreal values is stored on the STACK (because the keyword 'new' is not used), and the 'return points' is returning a pointer to the QList. But to me, it seems wrong to return a pointer to values on the stack that are going to become 'out-of-scope' after the function returns.

    Could some help explain what is going on with respect to the STACK and my out-of-scope concerns with this code that exists in the Qt library source code.

    Thanks.

    J J 2 Replies Last reply 4 Apr 2024, 13:03
    0
    • E EricR
      4 Apr 2024, 12:45

      While debugging a problem in my Qt application, I came across the following snippet of code (which I have shrunk for the purpose of this question) that I don't quite understand. The snippet is from the Qt 6.6.1 library source code.

      QList<qreal> ChartValueAxisY::calculateLayout() const
      {
          QList<qreal> points;
          for (int i = 0; i < 10; ++i)
          {
              points[i] = qreal(i);
          }
          return points;
      }
      

      My understanding is that the QList of qreal values is stored on the STACK (because the keyword 'new' is not used), and the 'return points' is returning a pointer to the QList. But to me, it seems wrong to return a pointer to values on the stack that are going to become 'out-of-scope' after the function returns.

      Could some help explain what is going on with respect to the STACK and my out-of-scope concerns with this code that exists in the Qt library source code.

      Thanks.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Apr 2024, 13:03 last edited by
      #2

      @EricR said in Is QList out-of-scope when function returns?:

      My understanding is that the QList of qreal values is stored on the STACK (because the keyword 'new' is not used), and the 'return points' is returning a pointer to the QList.

      First part is correct: your QList is allocated on the stack.
      Second part is wrong: calculateLayout returns QList by value, not a pointer - that means points is copied.
      If calculateLayout would return a pointer (QList<qreal>* ChartValueAxisY::calculateLayout()) then it would be a problem.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply 4 Apr 2024, 13:45
      2
      • E EricR
        4 Apr 2024, 12:45

        While debugging a problem in my Qt application, I came across the following snippet of code (which I have shrunk for the purpose of this question) that I don't quite understand. The snippet is from the Qt 6.6.1 library source code.

        QList<qreal> ChartValueAxisY::calculateLayout() const
        {
            QList<qreal> points;
            for (int i = 0; i < 10; ++i)
            {
                points[i] = qreal(i);
            }
            return points;
        }
        

        My understanding is that the QList of qreal values is stored on the STACK (because the keyword 'new' is not used), and the 'return points' is returning a pointer to the QList. But to me, it seems wrong to return a pointer to values on the stack that are going to become 'out-of-scope' after the function returns.

        Could some help explain what is going on with respect to the STACK and my out-of-scope concerns with this code that exists in the Qt library source code.

        Thanks.

        J Offline
        J Offline
        JonB
        wrote on 4 Apr 2024, 13:06 last edited by
        #3

        @EricR
        There is no "out of scope" here. The QList itself is stored on the stack, but all the contents are stored on the heap. Yes you can return a QList from a function. That is just a very small stack object, which is copied as a return result. And because of Qt's Implicit Sharing it actually just increments a counter, the elements/list are not actually copied a second time. But that part is incidental to your question.

        E 1 Reply Last reply 4 Apr 2024, 13:46
        2
        • J jsulm
          4 Apr 2024, 13:03

          @EricR said in Is QList out-of-scope when function returns?:

          My understanding is that the QList of qreal values is stored on the STACK (because the keyword 'new' is not used), and the 'return points' is returning a pointer to the QList.

          First part is correct: your QList is allocated on the stack.
          Second part is wrong: calculateLayout returns QList by value, not a pointer - that means points is copied.
          If calculateLayout would return a pointer (QList<qreal>* ChartValueAxisY::calculateLayout()) then it would be a problem.

          E Offline
          E Offline
          EricR
          wrote on 4 Apr 2024, 13:45 last edited by
          #4

          @jsulm Thanks for the explanation.

          1 Reply Last reply
          0
          • J JonB
            4 Apr 2024, 13:06

            @EricR
            There is no "out of scope" here. The QList itself is stored on the stack, but all the contents are stored on the heap. Yes you can return a QList from a function. That is just a very small stack object, which is copied as a return result. And because of Qt's Implicit Sharing it actually just increments a counter, the elements/list are not actually copied a second time. But that part is incidental to your question.

            E Offline
            E Offline
            EricR
            wrote on 4 Apr 2024, 13:46 last edited by
            #5

            @JonB Thanks for the explanation.

            1 Reply Last reply
            0
            • E EricR has marked this topic as solved on 4 Apr 2024, 13:46

            5/5

            4 Apr 2024, 13:46

            • Login

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