• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            攀升·Uranus


            Something Different,Something New
            數據加載中……

            返回內部靜態成員 熟練C/C++(一)

             

                    看完陳皓的C/C++返回內部靜態成員的陷阱,認識到自己確實對C/C++本身語法研究的不夠清楚,所以這些時間就在對基本知識進行回顧,真的還蠻有意思的。

                    我在用C/C++函數時,從沒有全面考慮過該函數功能,只是知道它能做,基本對函數細節沒有了解,就拿下面這個函數做個例子:

                    char *inet_ntoa(struct in_addr in);

                    struct in_addr {      unsigned long int s_addr; }

             

                    看到這個我就能想到該函數是把一個unsigned long type的數轉換成一個字符串。其它什么都不想。現在讓我們來仔細品讀里面的東西。

                    我傳入一個unsigned long type的數據,它給我傳出一個char *,那這個char * 在函數里怎么分配空間的。首先不可能是堆分配,因為如果是那樣的話,你用完這個函數后還要釋放資源。其次不可能是棧分配,因為那樣函數返回后棧也會跟著釋放。那還有可能是全局變量,如果這樣的話,C/C++中已經有好多全局了。那還有一種是static的可能,static不會隨著函數的返回而釋放,也就是說,它是一塊長期被分配的內存空間,現在在假若我在程序中這樣寫:

                    printf(“%s, %s”, inet_ntoa(a), inet_ntoa(b)); //a, b 是兩個不相等的值

                    輸出會讓我大吃一驚,輸出結果一樣。原因很簡單,就是printf先求b,把值給了那個static,然后再求a, 把值又給了staticstatic的那塊內存最終被寫入了a的值,這個時候輸出,那當然就輸出的同一個值了。還有一種錯誤寫法,如下:

             

                    Char *tmp1 = inet_ntoa(a);

                    Char *tmp2 = inet_ntoa(b);

                    這樣也是有問題的,因為tmp1tmp2都指向了一塊內存,當前的static的值就是b的值了。所以總結如下,使用這種函數一定要copy函數返回的值,而不能去保存其內存地址!
            附inet_ntoa()源碼:
            #include <stdio.h>
            #include <stdlib.h>
            #include <arpa/inet.h>
            #include <bits/libc-lock.h>

            /* The interface of this function is completely stupid, it requires a
               static buffer.  We relax this a bit in that we allow at least one
               buffer for each thread.  */


            /* This is the key for the thread specific memory.  */
            static __libc_key_t key;

            /* If nonzero the key allocation failed and we should better use a
               static buffer than fail.  */

            static char local_buf[18];
            static char *static_buf;                                //靜態

            /* Destructor for the thread-specific data.  */
            static void init (void);
            static void free_key_mem (void *mem);


            char *
            inet_ntoa (struct in_addr in)
            {
              __libc_once_define (static, once);
              char *buffer;
              unsigned char *bytes;

              /* If we have not yet initialized the buffer do it now.  */
              __libc_once (once, init);

              if (static_buf != NULL)
                buffer = static_buf;
              else
                {
                  /* We don't use the static buffer and so we have a key.  Use it
            to get the thread-specific buffer.  */

                  buffer = __libc_getspecific (key);
                  if (buffer == NULL)
            {
              /* No buffer allocated so far.  */
              buffer = malloc (18);
              if (buffer == NULL)
                /* No more memory available.  We use the static buffer.  */
                buffer = local_buf;
              else
                __libc_setspecific (key, buffer);
            }
                }

              bytes = (unsigned char *) &in;
              __snprintf (buffer, 18, "%d.%d.%d.%d",
                  bytes[0], bytes[1], bytes[2], bytes[3]);

              return buffer;
            }


            /* Initialize buffer.  */
            static void
            init (void)
            {
              if (__libc_key_create (&key, free_key_mem))
                /* Creating the key failed.  This means something really went
                   wrong.  In any case use a static buffer which is better than
                   nothing.  */

                static_buf = local_buf;
            }

            posted on 2007-04-13 16:07 攀升 閱讀(1608) 評論(1)  編輯 收藏 引用 所屬分類: C/C++

            評論

            # re: 返回內部靜態成員 熟練C/C++(一)   回復  更多評論   

            static讓我歡喜讓我憂。。。
            2007-04-13 18:56 | 田德健
            久久精品国产精品亚洲人人 | 国产激情久久久久影院老熟女| 亚洲国产精品无码久久98| 久久精品人人做人人爽电影蜜月| 东京热TOKYO综合久久精品| 久久成人精品| 久久夜色精品国产欧美乱| 久久久WWW免费人成精品| 狠狠色综合网站久久久久久久高清| 99久久国产综合精品麻豆| 久久亚洲欧洲国产综合| 久久精品亚洲精品国产色婷| 久久精品国产99国产精品| 亚洲国产精品无码久久一区二区| 94久久国产乱子伦精品免费| 久久无码人妻一区二区三区午夜| 久久99精品国产麻豆婷婷| 乱亲女H秽乱长久久久| 亚洲日韩欧美一区久久久久我| 久久精品国产亚洲av水果派 | 久久香综合精品久久伊人| 久久久久久亚洲精品成人| 亚洲精品久久久www| 国产成人久久777777| 国产成人久久AV免费| 久久精品国产亚洲αv忘忧草| 99热成人精品免费久久| 久久精品草草草| 欧美精品一本久久男人的天堂| 欧美喷潮久久久XXXXx| 奇米影视7777久久精品人人爽| 少妇被又大又粗又爽毛片久久黑人| 婷婷综合久久狠狠色99h| 国产69精品久久久久777| 久久久精品国产sm调教网站| 亚洲精品国产字幕久久不卡| 久久丫忘忧草产品| 久久棈精品久久久久久噜噜| 婷婷五月深深久久精品| 狠狠狠色丁香婷婷综合久久五月 | 久久伊人影视|