【发布时间】:2014-10-25 20:11:36
【问题描述】:
我在 fprintf 上遇到了这段代码的分段错误:
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
int fd;
int main(int argc, char **argv) {
fd = posix_openpt(O_RDWR | O_NOCTTY);
fprintf(fd, "hello\n");
close(fd);
}
但它适用于:
fprintf(stderr, "hello\n");
这是什么原因造成的?
【问题讨论】:
-
你检查 posix_openpt() 的结果了吗?
-
fprintf() 需要 FILE* 而不是文件描述符
-
你不应该忽略你不理解的警告......尤其是如果相应的行导致了段错误。
标签: c linux segmentation-fault pty