Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15210236
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: C/C++

2008-05-31 08:54:07

 看完陈皓的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
#include
#include
#include

/* 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];
[NextPage]

  else
    __libc_setspecific (key, buffer);
}
    }

  bytes = (unsigned char *) ∈
  __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;
}

阅读(320) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~