How exactly does the callstack work?

后端 未结 7 1474
庸人自扰
庸人自扰 2020-11-30 16:44

I\'m trying to get a deeper understanding of how the low level operations of programming languages work and especially how they interact with the OS/CPU. I\'ve probably read

7条回答
  •  离开以前
    2020-11-30 17:08

    Like others noted, there is no need to pop parameters, until they go out of scope.

    I will paste some example from "Pointers and Memory" by Nick Parlante. I think the situation is a bit more simple than you envisioned.

    Here is code:

    void X() 
    {
      int a = 1;
      int b = 2;
    
      // T1
      Y(a);
    
      // T3
      Y(b);
    
      // T5
    }
    
    void Y(int p) 
    {
      int q;
      q = p + 2;
      // T2 (first time through), T4 (second time through)
    }
    

    The points in time T1, T2, etc. are marked in the code and the state of memory at that time is shown in the drawing:

    enter image description here

提交回复
热议问题