亚洲喷奶水中文字幕电影,日本aⅴ高清一区二区三区,欧美亚洲日本国产,欧美日韩亚洲中文字幕

<legend id="flx4p"><abbr id="flx4p"><thead id="flx4p"></thead></abbr></legend>

<mark id="flx4p"><thead id="flx4p"></thead></mark>

      C/C++有關(guān)內(nèi)存的面試思考題

        void GetMemory(char *p)

        {

        p = (char *)malloc(100);

        }

        void Test(void)

        {

        char *str = NULL;

        GetMemory(str);

        strcpy(str, “hello world”);

        printf(str);

        }

        請問運行Test 函數(shù)會有什么樣的結(jié)果?

        答:程序崩潰。

        因為GetMemory 并不能傳遞動態(tài)內(nèi)存,Test 函數(shù)中的 str 一直都是 NULL。

        strcpy(str, “hello world”);將使程序崩潰。

        char *GetMemory(void)

        {

        char p[] = “hello world”;

        return p;

        }

        void Test(void)

        {

        char *str = NULL;

        str = GetMemory();

        printf(str);

        }

        請問運行Test 函數(shù)會有什么樣的結(jié)果?

        答:可能是亂碼。

        因為GetMemory 返回的是指向“棧內(nèi)存”的指針,該指針的地址不是 NULL,但其原現(xiàn)的內(nèi)容已經(jīng)被清除,新內(nèi)容不可知。

        void GetMemory2(char **p, int num)

        {

        *p = (char *)malloc(num);

        }

        void Test(void)

        {

        char *str = NULL;

        GetMemory(&str, 100);

        strcpy(str, “hello”);

        printf(str);

        }

        請問運行Test 函數(shù)會有什么樣的結(jié)果?

        答:(1)能夠輸出hello;(2)內(nèi)存泄漏

        void Test(void)

        {

        char *str = (char *) malloc(100);

        strcpy(str, “hello”);

        free(str);

        if(str != NULL)

        {

        strcpy(str, “world”);

        printf(str);

        }

        }

        請問運行Test 函數(shù)會有什么樣的結(jié)果?

        答:篡改動態(tài)內(nèi)存區(qū)的內(nèi)容,后果難以預(yù)料,非常危險。

        因為free(str);之后,str 成為野指針,if(str != NULL)語句不起作用。

      本文已影響6827
      上一篇:關(guān)于C以及C++的幾個面試題 下一篇:C語言面試題

      相關(guān)文章推薦

      |||||