内存四个区域:
1代码区:
可执行代码
2静态区:
所有静态变量和全局变量
增长方向 从高向低
3堆区:
增长方向 从低向高
4栈区
自动变量,形参
*堆内存分配函数区别:
char *p =malloc(10);//分配空间,但未清洁;
memset(p,0,10);//使用memset清洁;
char *p1=calloc(10,sizeof(char));//分配10个char字节空间,并进行清洁;
char *p2=realloc(P1,20);//在原有内存之上,在堆内存增加连续内存。如果原有内存不足,那么开辟新的空间,将原有内存copy过来; realloc()函数也不做清洁;
free(p);//malloc 和calloc 都要进行free;
测试堆栈增长方向代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=5;
int b=8;
int c=9;
printf("strack\n");
printf("a'addr=[%d]\n",&a);
printf("d'addr=[%d]\n",&b);
printf("c'addr=[%d]\n",&c);
char *d=malloc(1000*sizeof(char));
char *e=malloc(1000*sizeof(char));
char *f=malloc(1000*sizeof(char));
printf("heap\n");
printf("d'addr=[%d]\n",d);
printf("e'addr=[%d]\n",e);
printf("f'addr=[%d]\n",f);
return 0;
}