堆和栈
1、堆的增长方向是递增;
2、栈的增长方向是递减。
实验代码:
printf("栈操作:\n");
int num1 = 10;
int num2 = 11;
printf("num1:%d\n", num1);
printf("num2:%d\n", num2);
printf("&num1:%d\n", &num1);
printf("&num2:%d\n", &num2);//栈操作
printf("堆操作:\n");
int *num3;
int *num4;
num3 = new int;
num4 = new int;
*num3 = 12;
*num4 = 13;
printf("*num3:%d\n", *num3);
printf("*num4:%d\n", *num4);
printf("num3:%d\n", num3);
printf("num4:%d\n", num4);
printf("&num3:%d\n", &num3);
printf("&num4:%d\n", &num4);
delete num3;
delete num4;
实验的结果如下:
在结果中:
&num1、&num2、&num3、&num4四个地址是在栈中的,num3、num4两个地址是在堆中的。