为什么而作C READDIR手册页说不是静态分配的结果结构拨打免费 [英] why does the C readdir man page say to not call free on the static allocated result struct

查看:228
本文介绍了为什么而作C READDIR手册页说不是静态分配的结果结构拨打免费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$的uname -a

$ uname -a

Linux crowsnest 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 UTC 2011 x86_64 GNU/Linux

$男人READDIR:

$ man readdir:

说明

()函数返回一个指针的dirent结构重新presenting目录流中的下一个目录条目READDIR指向
         dirp ...

The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp...

.. [剪断] ...

..[snip]...

在readdir_r()函数是READDIR()...

The readdir_r() function is a reentrant version of readdir()...

... [剪断] ...

...[snip]...

返回值

在成功READDIR()返回一个指针的dirent结构。 (该结构可以静态分配;不要尝试释放(3)。)如果达到了目录流的末尾,则返回NULL,并将errno没有改变。如果发生错误,则返回NULL,并将errno设置为合适。

On success, readdir() returns a pointer to a dirent structure. (This structure may be statically allocated; do not attempt to free(3) it.) If the end of the directory stream is reached, NULL is returned and errno is not changed. If an error occurs, NULL is returned and errno is set appropriately.

在readdir_r()函数成功返回0。在错误,它会返回一个正的错误编号。如果到达目录流的结尾,readdir_r()返回0,而在*结果返回NULL。

The readdir_r() function returns 0 on success. On error, it returns a positive error number. If the end of the directory stream is reached, readdir_r() returns 0, and returns NULL in *result.

我很困惑这是什么意思,我该功能的应用程序是收集指针的动态分配的数组包含有关目录条目的数据结构,我想知道我是否可以动态分配的dirent结构,并设置指针它们。但此行的接缝说,结果应该不会被自由调用,所以我不知道我是否应该分配一个单独的dirent结构,这将是列表的一部分和memcpy它在返回的结果。

I'm confused about what this means, my application of this function is to collect a dynamically allocated array of pointers to structs with data about the directory entries, and I'm wondering if I can dynamically allocate dirent structs and set the pointers to them. but this line seams to say that the result should never be called by free, so I'm wondering if I should allocate a seperate dirent struct which will be part of the list and memcpy it over the returned result.

我也的术语混淆可能在上面的手册页。这是否意味着somtimes它是静态分配的,有时不是。

I'm also confused by the terminology of "may" in the above man page. does this mean that somtimes it's statically allocated, and sometimes it's not.

我很熟悉,(隐约)什么静态变量在C均值,但不知道所有的规则和可能的疑难杂症的角落找寻他们。因为我想通过的dirent结构是在一个目录的时候,我宁愿它可以动态分配。请问这是什么readdir_r是?或将双指针设置为指向另一个静态分配的dirent结构?

I'm familiar, (vaguely) with what static variables mean in C, but not sure about all the rules and possible gotcha's arround them. because I want to pass the dirent structs that are in a directory around, I would rather it be dynamically allocated. is this what readdir_r is for? or will the double pointer be set to point to another statically allocated dirent struct?

和我不能完全肯定意味着在这方面的readdir_r什么折返。我renetrant的理解只是从计划协同程序,我不知道如何将适用于阅读的Unix目录。

and I'm not entirely sure what reentrant means in this context for readdir_r. my understanding of renetrant is only from scheme coroutines which I'm not sure how that would apply to reading unix directories.

推荐答案

这里的规则是非常简单的 - 你可以自由地进行数据的拷贝 READDIR()的回报,但是你不拥有它把在数据,因此你不能采取建议你做动作的缓冲。 (即,将数据复制到您自己的缓冲区;不要存放指针READDIR拥有的缓冲区内)

The rule here is really simple -- you're free to make a copy of the data readdir() returns, however you don't own the buffer it puts that data in so you cannot take actions that suggest you do. (I.e., copy the data out to your own buffer; don't store a pointer to within the readdir-owned buffer.)

所以我不知道我是否应该分配一个单独的dirent结构,这将是列表的一部分和memcpy它在返回的结果 - 这正是你应该做的

so I'm wondering if I should allocate a seperate dirent struct which will be part of the list and memcpy it over the returned result - that's exactly what you should do.

我还的术语混淆可能在上面的手册页。这是否意味着somtimes它是静态分配的,有时它不是 - 这意味着你无法将如何管理计数,但它会为你进行管理。细节可以变化从一个系统到下一个。

I'm also confused by the terminology of "may" in the above man page. does this mean that somtimes it's statically allocated, and sometimes it's not. - it means you cannot count on how it will be managed, but it will be managed for you. The details could vary from one system to the next.

折返意味着线程安全的。 READDIR()使用静态项,使其无法安全的多线程,如果他们每个控制多方通话过程中使用。 readdir_r()将利用分配调用者提供空间,让多线程独立行事。

Reentrant means thread-safe. readdir() uses a static entry, making it not safe for multiple threads to use as if they each control the multi-call process. readdir_r() will use allocated space provided by the caller, letting multiple threads act independently.

这篇关于为什么而作C READDIR手册页说不是静态分配的结果结构拨打免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆