【问题标题】:Segmentation fault in strlen when using FTS fts_open()使用 FTS fts_open() 时 strlen 中的分段错误
【发布时间】:2013-03-06 04:30:18
【问题描述】:

我发现一个错误归结为:

#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <stdlib.h>

int main () {
    char *LOG_ROOT = "/var/log";

    FTS *ftsp; 
    FTSENT *p, *chp;
    int fts_options = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR;

    char *paths[] = { LOG_ROOT };
    fts_open(paths, fts_options, NULL);
}

为什么会出现这个段错误?

【问题讨论】:

  • 为这个添加了太相关的“数组”标签。

标签: c arrays segmentation-fault traversal strlen


【解决方案1】:

第一个参数。应为 NULL 终止的数组 字符指针。

char *paths[] = { LOG_ROOT, NULL};

【讨论】:

  • 你现在是我的绝对英雄。我一直盯着 1000 行代码 2 个小时试图找到问题所在。注释掉随机行可以解决问题,因为它们会使该位置为 NULL。
猜你喜欢
  • 2016-03-25
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多