Question Details

No question body available.

Tags

c posix glob

Answers (1)

April 21, 2026 Score: 4 Rep: 28,665 Quality: Medium Completeness: 90%

From the POSIX specification for glob:

If glob() terminates due to an error, it shall return one of the non-zero constants defined in . The arguments pglob->glpathc and pglob->glpathv are still set as defined above.

Since pglob->gl_pathv is an element that is likely to have memory allocated for it:

  • We need to call globfree() to ensure that, if memory has been allocated, it will be freed, and
  • Since it's guaranteed to have been initialised, we can be confident that we are not using an uninitialised value (undefined behaviour) in doing so.

So the answer to the question is "always call globfree() after calling glob()".