<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            親情的句子

            時間:2025-11-23 13:48:31 好詞好句 我要投稿

            關于親情的句子大全

               中興筆試問題指針出錯,請指出這個程序里的不正確的地方

            關于親情的句子大全

              char *getString()

              {

              return (char*)malloc(800);

              }

              int main()

              {

              char *str;

              str = getString();

              pintf("%s",str);

              return 0;

              }

              原來的參考答案:

              錯誤在于:

              在前面的函數中。malloc分出來的空間被放在了一個棧上面

              主函數使用了這個函數后操作系統后彈出棧頂

              也就是這塊空間被shi放了(9分)

              后面那個printf打印的本來是指向棧頂的那個地址的。。

              但現在彈出了

              所以不知道指到哪里去了

              也不會得到自己想要的打印結果(9分)

              個人認為:

              這個根本就不是在棧上,而是在推上,用函數返回堆的地址,這是很常見的做法啊,大家可以參照高質量編程。

              這個我覺得不會是中興給的答案吧

              感覺太水了。不知道大家看出來多少?

              你的想法是正確的。

              在一個函數中malloc一段內存,供另一個函數使用,沒有任何問題。

              msdn原文如下:

              void *malloc(size_t size );

              malloc returns a void pointer to the allocated space or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.

              可見,分配是在堆上(heap),而不是在棧上。

              但是,對于這段代碼,有兩個問題需要注意:

              1。當內存不再使用時,應使用free()函數將內存塊釋放。其實,free這個函數大多人都會調用,但是free了并不代表完事了,一定要把指向這個堆空間的指針清空,這個題目里應使用str=NULL。

              2。malloc只管分配內存,并不能對所得的內存進行初始化,所以得到的一片新內存中,其值將是隨機的。這道題目,打印出來是亂碼。需要自動初始化,可以使用calloc函數。

            【親情的句子】相關文章:

            關于親情的優美句子(精選190句)05-07

            關于親情的作文:回報親情五-親情作文01-11

            有關親情的句子和段落摘抄180句07-14

            別樣的親情04-08

            親情作文08-18

            以親情作文01-24

            親情的作文06-29

            親情散文11-09

            親情的作文08-29

            實用的親情的作文07-12

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院