memory-alignment

Allocating a struct dirent without malloc()

折月煮酒 提交于 2020-12-12 11:38:47
问题 I need to use readdir_r() to read the contents of a directory in a multithreaded program. Since the size of struct dirent is filesystem dependent, man readdir_r recommends name_max = pathconf(dirpath, _PC_NAME_MAX); if (name_max == -1) /* Limit not defined, or error */ name_max = 255; /* Take a guess */ len = offsetof(struct dirent, d_name) + name_max + 1; to find the size of the allocation needed. To allocate it entryp = malloc(len); is called, and finally readdir_r() uses it like this: