内存的几个小问题

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

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