Viewing 4 reply threads
  • Author
    Posts
    • #6143 Score: 0
      FlowingCrescent
      Participant
      3 pts

      排查可知,当给masses[i]赋值,运行程序就会发生“Segmentation fault(core dumped)”,然后程序闪退……由于本次的想法也很简单,找来找去也没发现哪里有错,个人觉得可能是因为我不熟悉C++,而在哪里的写法上出了问题,还请各位大神不吝赐教……

      Attachments:
      You must be logged in to view attached files.
    • #6145 Score: 0
      arc
      Participant
      5 pts

      这样创建的 m 和 s 的生命周期仅仅在循环体中,每次退出循环都会将它们销毁。这样直接取指针会导致之后使用已销毁的对象,产生运行时错误。
      你需要使用动态内存分配的方法对对象的生命周期进行管理。
      masses[i] = new Mass(position, node_mass, false), 下面的 springs 也类似。

      • #6147 Score: 0
        FlowingCrescent
        Participant
        3 pts

        原来如此,非常感谢!而我发现还需要记得把两个Vector都给resize一下,不然也依旧会报错,但是第一步做完现在只能看见3个分开的点,并不是一根线,我想问一下这个情况是所预期的吗?

        • #6148 Score: 0
          FlowingCrescent
          Participant
          3 pts

          啊,已经解决了,没事了~

          • #6155 Score: 0
            Scott
            Participant
            -3 pt

            请问你是咋解决的呀,我还是会一直出现这个报错

            • #6165 Score: 0
              FlowingCrescent
              Participant
              3 pts

              在我的那张图的基础上,我改成了层主所说的masses[i] = new Mass(position, node_mass, false),不过在那之前,我resize了一下俩vector的长度,如果不resize的话我也依然会报错

              • #6170 Score: 0
                Bear
                Participant
                -11 pt

                std::vector 加元素得用 push_back 或者 emplace_back。。。
                直接 resize 不好

    • #6149 Score: 0
      o_o_o_o_o
      Participant
      -1 pt

      恩, 顺带马后炮一下 看到vector就应该想着 push_back

    • #6157 Score: 0
      chihiro
      Participant

      我也出现了Segmentation fault (core dumped)实在找不出哪里的问题 可以帮忙看看嘛

      Attachments:
      You must be logged in to view attached files.
      • #6162 Score: 0
        water
        Participant
        2 pts

        pinned_nodes不用塞数据

        • #6163 Score: 0
          chihiro
          Participant

          感谢!还想问一下这个语句为什么加上了还是会报错Segmentation fault (core dumped)呢

          Attachments:
          You must be logged in to view attached files.
          • #6166 Score: 0
            YY
            Participant
            1 pt

            看你之前的截图,这条语句你好像加在了构造节点之前了,是这样吗?

            • #6167 Score: 0
              chihiro
              Participant

              是的

              • #6201 Score: 0
                YY
                Participant
                1 pt

                应该放在节点构造之后,masses没有初始化。

    • #6369 Score: 0
      杰哥
      Participant
      2 pts

      顺便求问各位大佬,框架里面这段被注释掉的代码想表达什么意思呢?这个for (auto &i:)为什么可以这么写。

      Attachments:
      You must be logged in to view attached files.
Viewing 4 reply threads
  • You must be logged in to reply to this topic.