内存的几个小问题

来源:互联网 发布:周扬青 淘宝 编辑:程序博客网 时间:2024/05/22 01:56

先看问题,下面只我最近刷题发现的几个高频题

char *GetMemory(void){    char p[] = "hello world";    printf("%s\n", p);    return p;}void GetMemory1(char *p){    p = (char *)malloc(100);}void GetMemory2(char *&p, int num){    p = (char *)malloc(num);}void Test(void){    char*str = NULL;    printf("%x\n", str);    str = (char *)malloc(100);    printf("%x\n", str);    //str = NULL;    strcpy(str, "hello");    printf("%x\n", str);    free(str);    printf("%x\n", str);    if (str != NULL)    {        strcpy(str, "word");        printf("%s\n",str);    }}