程序内存大致分四块如下:
下面以两个例子来解释堆栈和堆如何在程序中体现:
#include <iostream>int add(int a, int b){int sum = a + b;return aum;
}int main(){int x = 5;int result = add(x, 10);std::cout << "Result: " << result << std::endl;return 0;
}
#include <iostream>int mian(){int value = 42;int *ptr = new int;*ptr = value;std::<<"Value: "<< *ptr << std::endl;delete ptr;return 0;
}
堆栈区的主要区别:
更详细的内容可以参考https://mp.weixin.qq.com/s/0GOyRUIrKX5-2h3YNKag3g