ubuntu

Ubuntu中copendir的内存管理技巧

小樊
44
2025-12-13 09:23:32
栏目: 智能运维

Ubuntu 中 opendir 的内存管理要点

一 核心原则与最小示例

示例(C,最小可运行骨架):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    if (argc != 2) return 1;
    const char *path = argv[1];

    DIR *dir = opendir(path);
    if (!dir) { perror("opendir"); return 1; }

    struct dirent *ent;
    // 按需设置缓冲大小;NAME_MAX 在 <limits.h> 中定义
    size_t bufsz = pathconf(path, _PC_NAME_MAX) + sizeof(struct dirent) + 1;
    if (bufsz == -1) bufsz = 4096;  // 回退
    char *buf = malloc(bufsz);
    if (!buf) { closedir(dir); return 1; }

    struct dirent *entry;
    int ret = 0;
    while ((readdir_r(dir, (struct dirent *)buf, &entry) == 0 && entry)) {
        // 跳过 "." 和 ".."
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;

        // 构造目标路径:复用栈上小缓冲 + 按需分配
        size_t len = strlen(path) + 1 + strlen(entry->d_name) + 1;
        char *dst = len < 4096 ? alloca(len) : malloc(len);
        if (!dst) { ret = 1; break; }
        snprintf(dst, len, "%s/%s", path, entry->d_name);

        // 示例:仅打印;实际可加入 stat/lstat、mkdir/open/write 等
        puts(dst);

        if (dst != (char*)alloca(len)) free(dst);
    }

    free(buf);
    if (closedir(dir) != 0) { perror("closedir"); ret = 1; }
    return ret;
}

要点:

二 常见内存与资源问题及规避

三 检测与排查工具

四 性能与稳定性优化

0
看了该问题的人还看了