#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//栈方向
void func()
{
printf("func:\n");
int a,b,c,d;
printf("%p\n",&a);
printf("%p\n",&b);
printf("%p\n",&c);
printf("%p\n",&d);
return;
}
int main()
{
printf("main:\n");
int a,b,c,d;
printf("%p\n",&a);
printf("%p\n",&b);
printf("%p\n",&c);
printf("%p\n",&d);
func();
return 0;
}
//栈溢出
void func(int n)
{
if(n == 0)
return;
else
func(n-1);
}
int main()
{
//char arr[1024*1024*10];
//strcpy(arr,"china");
func(1000000000); //栈溢出,导致段错误
printf("over\n");
return 0;
}
//堆方向
void func()
{
printf("in func:\n");
char *pa,*pb,*pc,*pd;
pa=malloc(100);
pb=malloc(100);
pc=malloc(100);
pd=malloc(100);
printf("%p\n",pa);
printf("%p\n",pb);
printf("%p\n",pc);
printf("%p\n",pd);
}
int main()
{
printf("in main:\n");
char *pa,*pb,*pc,*pd;
pa=malloc(100);
pb=malloc(100);
pc=malloc(100);
pd=malloc(100);
printf("%p\n",pa);
printf("%p\n",pb);
printf("%p\n",pc);
printf("%p\n",pd);
func();
return 0;
}
//堆的简单使用
int main (int argc, char *argv[])
{
char * p = (char*)malloc(1024*1024*1024);
if(p == NULL)
printf("malloc error\n");
strcpy (p,"abcd");
printf("over\n");
free(p);
p=NULL;
return 0;
}