Reputation: 987
readdir
return a pointer to a struct dirent
, I have tested if two calls to readdir
stored in two different pointers modify the first pointer content, it doesn't.
So I guess readdir allocate some memory, am I right ?
If so, it should be released, something I never did before...
Upvotes: 0
Views: 1290
Reputation: 121427
POSIX says that an application must not modify the structure returned by readdir()
:
The application shall not modify the structure to which the return value of readdir() points, nor any storage areas pointed to by pointers within the structure. The returned pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by a subsequent call to readdir() on the same directory stream. They shall not be affected by a call to readdir() on a different directory stream.
So whether it internally allocates memory or uses a static buffer depends on how how a specific implementation implements. All you need to do is respect the contract that the function requires. That is, you must not attempt to modify it and call closedir()
to close the directory stream.
Upvotes: 2
Reputation: 5289
Yup, it should be released. You should call closedir
on a directory after readdir
, just like you call fclose
after fopen
for regular files.
Upvotes: 1
Reputation: 60037
If it does allocated any memory the call to closedir (http://linux.die.net/man/3/closedir) will free it up for you
Upvotes: 1