Community
    • 登入

    MenuIcon problem with latest version of NPP

    已排程 已置頂 已鎖定 已移動 Notepad++ & Plugin Development
    25 貼文 6 Posters 18.1k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • YaronY
      Yaron
      最後由 編輯

      Hello Pavel,

      ret is a local variable of returnIntPointer() after all.

      I understand that.
      My idea was that returnIntPointer() is not destroyed until the following ; is reached.
      IOW: since we use useIntPointer(returnIntPointer());, it’s valid until we return to );.

      Otherwise, is a new temporary copy of ret created?
      And what’s the exact definition? You can copy it just at the very beginning of the function? Isn’t it a bit far fetched?

      Another option is that you assume there’s not enough time to destroy ret because of the events proximity.
      But that doesn’t make sense either.

      I suppose this question can be tested.

      Thanks again. I appreciate your contribution to MenuIcons as well. :)
      BR

      1 條回覆 最後回覆 回覆 引用 0
      • pnedevP
        pnedev
        最後由 pnedev 編輯

        Hello Yaron,

        My idea was that returnIntPointer() is not destroyed until the following ; is reached.
        IOW: since we use useIntPointer(returnIntPointer());, it’s valid until we return to );

        That’s true for returnInteger() not for returnIntPointer() and returnIntPointer() is a function - it is not destroyed. The value (variable) it returns is not destroyed until the next ;.

        Let’s look at returnIntPointer()'s code:

        int * returnIntPointer()
        {
            int ret = 5;
            return &ret;
        }
        

        When we enter the function, ret is created on the stack, the stack pointer is moved (let’s say incremented for clarity).
        Now we return ret’s address and exit the function. When we exit, ret is freed -> the stack pointer is decremented.
        Now the returned pointer points to free stack memory that will hold the old ret’s value until another local (automatic) variable is created and that usually happens when you enter another function. In our case this function is useIntPointer(). So in useIntPointer() our pointer points to invalid data.

        When you return ret by value (as in returnInteger()) the returned value is either in processor register or on the stack before all returnInteger() local variables are put there and later - destroyed. IOW there is no copy performed - it is simply a compiler matter.

        Your assumption is correct to the point that whatever variable a function returns (temporary) lives until the next ; where the function is called.
        In returnIntPointer() the returned variable is int* and it lives until next ; after the call but it points to what? Some free memory that is overwritten perhaps and assigned to another variable.
        In returnInteger() the returned variable is int and it’s just what you need (it is not extra-copied on return, don’t worry, it’s just created by the compiler in the appropriate way). You can assign it to another variable to store it or directly give it to another function (copying it or using it’s address - doesn’t matter) - it is valid until the next ; after the call.

        Hope that clarifies.

        BR

        1 條回覆 最後回覆 回覆 引用 1
        • YaronY
          Yaron
          最後由 編輯

          Hello Pavel,

          The issue is clearer now.
          Some of the terms you’ve used are new to me. I’ll have to expand my basic knowledge. :)

          Thank you for the interesting explanation. As always, I appreciate your patience.

          BR

          1 條回覆 最後回覆 回覆 引用 2
          • Claudia FrankC
            Claudia Frank
            最後由 編輯

            Hello Yaron and Pavel,

            coming back from a short trip it is nice to see that you got it sorted out.

            Pavel, thank you very much for your insights - very much appreciated.
            Your explanation gives me the feeling that I’ve done another step in
            understanding this pointer stuff, although it seems to be more like
            an variable lifetime issue. :-)

            Yaron, thanks for asking the questions I would have asked also. :-)

            Cheers
            Claudia

            1 條回覆 最後回覆 回覆 引用 1
            • YaronY
              Yaron
              最後由 編輯

              Hello Claudia,

              Welcome back. I hope you had a good time.
              The forum is not what it is when you’re away. :)

              Thanks again for your kind help.

              Best regards.

              1 條回覆 最後回覆 回覆 引用 0
              • 第一個貼文
                最後的貼文
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors