#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#if 1
char* getMen2() {char buf[64]; //临时变量,栈区存放strcpy(buf, "abccddeeff");printf("buf:%s\n", buf);return buf; //此处并不是把内存块64个字节return,而是把内存块的首地址返回给tmp
}
char* getMen1(num) {char* p;p = (char *)malloc(sizeof(char)*num); //全局变量,程序结束时,操作系统释放内存strcpy(p, "abccddeeff");return p;
}
void main()
{char* tmp = NULL;tmp = getMen2();printf("tmp:%s\n", tmp); //空间已经被释放。输出乱码char* tmp1 = NULL;tmp1 = getMen1(10);printf("tmp1:%s\n", tmp1);
}
#endif