Community
    • Login

    MenuIcon problem with latest version of NPP

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    25 Posts 6 Posters 17.3k Views
    Loading More Posts
    • 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.
    • YaronY
      Yaron
      last edited by

      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 Reply Last reply Reply Quote 0
      • pnedevP
        pnedev
        last edited by 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 Reply Last reply Reply Quote 1
        • YaronY
          Yaron
          last edited by

          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 Reply Last reply Reply Quote 2
          • Claudia FrankC
            Claudia Frank
            last edited by

            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 Reply Last reply Reply Quote 1
            • YaronY
              Yaron
              last edited by

              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 Reply Last reply Reply Quote 0
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors