方法调用时栈内存的分配调用:
对Stack frame的分析:
Stack Frame:一个方法在调用的时候在栈内存中的布局。
class Program
{
static void Main(string[] args)
{
double result = Calculator.GetConeVolume(100, 80);
}
}
class Calculator
{
public static double GetCircleArea(double r)
{
return Math.PI * r * r;
}
public static double GetCylinderVolume(double r,double h)
{
double a = GetCircleArea(r);
return a * h;
}
public static double GetConeVolume(double r,double h)
{
double cv = GetCylinderVolume(r, h);
return cv / 3;
}
}
主程序运行从Main开始执行。
程序中main方法作为主调用者caller。GetConeVolume叫做被调用者callee。main方法在调用GetConeVolume时需要传两个参数进去。传的参数也是变量,变量也需要在内存中存储。

本文详细解析了C#和C++中函数调用时栈内存的分配过程,包括StackFrame的作用,参数管理规则,以及递归调用中的空间管理,防止StackOverflow。重点介绍了返回值处理和内存释放机制。
最低0.47元/天 解锁文章
3741





