免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1681 | 回复: 3
打印 上一主题 下一主题

请教一个glib函数的源码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-04 03:40 |只看该作者 |倒序浏览
在下面的源码里,为什么用二维指针contents而不是一维指针作为参数?谢谢。
static gboolean
get_contents_regfile (const gchar *display_filename,

                      struct stat *stat_buf,

                      gint         fd,

                      gchar      **contents,

                      gsize       *length,

                      GError     **error)

{

  gchar *buf;

  gsize bytes_read;

  gsize size;

  gsize alloc_size;



  size = stat_buf->st_size;



  alloc_size = size + 1;

  buf = g_try_malloc (alloc_size);



  if (buf == NULL)

    {

      g_set_error (error,

                   G_FILE_ERROR,

                   G_FILE_ERROR_NOMEM,

                   _("Could not allocate %lu bytes to read file \"%s\""),

                   (gulong) alloc_size,

                   display_filename);



      goto error;

    }



  bytes_read = 0;

  while (bytes_read < size)

    {

      gssize rc;



      rc = read (fd, buf + bytes_read, size - bytes_read);



      if (rc < 0)

        {

          if (errno != EINTR)

            {

              int save_errno = errno;



              g_free (buf);

              g_set_error (error,

                           G_FILE_ERROR,

                           g_file_error_from_errno (save_errno),

                           _("Failed to read from file '%s': %s"),

                           display_filename,

                           g_strerror (save_errno));



              goto error;

            }

        }

      else if (rc == 0)

        break;

      else

        bytes_read += rc;

    }


  buf[bytes_read] = '\0';



  if (length)

    *length = bytes_read;



  *contents = buf;



  close (fd);



  return TRUE;



error:



  close (fd);



  return FALSE;

}

[ 本帖最后由 greencow 于 2009-1-4 03:43 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-01-04 03:49 |只看该作者
修改的是指针所指的地址,而不是指针指向地址的内容。
所以要用**

论坛徽章:
0
3 [报告]
发表于 2009-01-04 08:57 |只看该作者
我都没看到**

论坛徽章:
0
4 [报告]
发表于 2009-01-04 10:43 |只看该作者
原帖由 Roemer 于 2009-1-4 03:49 发表
修改的是指针所指的地址,而不是指针指向地址的内容。
所以要用**

明白了,多谢指点。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP