MenuIcon problem with latest version of NPP
-
Hello Pavel,
ret
is a local variable ofreturnIntPointer()
after all.I understand that.
My idea was thatreturnIntPointer()
is not destroyed until the following;
is reached.
IOW: since we useuseIntPointer(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 -
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 forreturnIntPointer()
andreturnIntPointer()
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 returnret
’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 oldret
’s value until another local (automatic) variable is created and that usually happens when you enter another function. In our case this function isuseIntPointer()
. So inuseIntPointer()
our pointer points to invalid data.When you return
ret
by value (as inreturnInteger()
) the returned value is either in processor register or on the stack before allreturnInteger()
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.
InreturnIntPointer()
the returned variable isint*
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.
InreturnInteger()
the returned variable isint
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
-
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
-
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 -
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.