Inline relation to stack


When I call an inline function (assuming it's properly compiled as inline), what is the behavior on the stack?

For example,

1
2
3
4
5
6
inline void InlineFun(){
    int local=0;
}
void Fun(){
    InlineFun();
}


I would assume local will exist on Fun()'s stack, but does it remain so after InlineFun() return or does it get erased as usual?

Thanks.
The program behavior is not allowed to change by inlining. That means the local variables will have to be destroyed when InlineFun returns (inlining or not).

Of course, "destroying" is a purely formal term. As long as the program behaves as if the variable was destroyed, the compiler can do whatever it wants.
An optimizing compiler will likely defer the actual "destruction" of "local" (which is just adding to the stack pointer in this case) to the end of Fun(). But since "local" is not used anywhere, no variable will be created or destroyed anyway.
Okay that makes sense.
One more question,
if I had changed it to int* local = new int;
then this would instead exist in the heap?
Well, the "local" pointer is still local, but the int you created with new will continue existing in the heap/free store after the function exits.
Topic archived. No new replies allowed.