內存的幾個小問題

先看問題,下面只我最近刷題發現的幾個高頻題

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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章