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. Why does recursion appear in QVector?
Forum Update on Tuesday, May 27th 2025

Why does recursion appear in QVector?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 158 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    John Van
    wrote on 12 Feb 2025, 05:44 last edited by John Van 2 Dec 2025, 05:50
    #1

    In this code, recursion appears in QVector, but not in std::vector.

    struct TreeNode
    {
        QString name;
        QVector<TreeNode> nodes;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        TreeNode n0{ "n0" };
        n0.nodes.push_back(n0);
        n0.nodes[0].nodes.push_back(n0);
    
    }
    

    &n0 0x0000002fa1bcf6e8 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0].nodes[0].nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
    &n0.nodes[0].nodes[0].nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }

    J 1 Reply Last reply 12 Feb 2025, 06:09
    0
    • J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 12 Feb 2025, 07:37 last edited by J.Hilk 2 Dec 2025, 07:41
      #3

      That's surprising, both should show recursive behaviour. Because you modify the nodes wich would trigger a copy of the vector element.
      ~
      Actually std::vector has no implizit sharing, it does the copy directly. Probably the reason why you see different behaviour.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • J John Van
        12 Feb 2025, 05:44

        In this code, recursion appears in QVector, but not in std::vector.

        struct TreeNode
        {
            QString name;
            QVector<TreeNode> nodes;
        };
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            TreeNode n0{ "n0" };
            n0.nodes.push_back(n0);
            n0.nodes[0].nodes.push_back(n0);
        
        }
        

        &n0 0x0000002fa1bcf6e8 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0].nodes[0].nodes[0] 0x0000013ce627d2f8 {name=n0 nodes={ size = 1 } }
        &n0.nodes[0].nodes[0].nodes[0].nodes[0] 0x0000013ce627e878 {name=n0 nodes={ size = 1 } }

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 12 Feb 2025, 06:09 last edited by
        #2

        @John-Van What recursion do you mean? This code runs just fine. Why you're adding n0 into its own nodes vector and then again into nodes[0] is another question. Is there a reason to do this?

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

        1 Reply Last reply
        2
        • J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 12 Feb 2025, 07:37 last edited by J.Hilk 2 Dec 2025, 07:41
          #3

          That's surprising, both should show recursive behaviour. Because you modify the nodes wich would trigger a copy of the vector element.
          ~
          Actually std::vector has no implizit sharing, it does the copy directly. Probably the reason why you see different behaviour.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • J John Van has marked this topic as solved on 14 Feb 2025, 09:15

          1/3

          12 Feb 2025, 05:44

          • Login

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