分类: LINUX
2013-02-20 16:50:19
程序1:
#include#include #include void getMemory(char *ptr) { ptr = (char *)malloc(100); if(NULL == ptr) { printf("malloc memory failed\n"); return ; } strcpy(ptr, "hello"); } int main(int argc, const char *argv[]) { char *str = NULL; getMemory(str); printf("str = %s\n", str); free(str); return 0; }
运行结果:str = (null)
程序2:
#include#include #include void getMemory(char *ptr) { ptr = (char *)malloc(100); if(NULL == ptr) { printf("malloc memory failed\n"); return ; } } int main(int argc, const char *argv[]) { char *str = NULL;
getMemory(str);
strcpy(str, "hello");
printf("str = %s\n, str");
free(str); return 0; }
运行结果:Segmentation fault