【发布时间】:2014-12-06 12:39:48
【问题描述】:
你好,我写了我的 c 程序,它将在 linux 上运行。 我正在尝试为 linux 制作自己的 shell。 我在下面有以下代码...
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
int main(void){
int i = 0;
int k = 0;
int argsCount = 0;
char inputBuffer[MAX_LINE]; /*buffer to hold command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2 + 1]; /*command line arguments */
pid_t tpid ;
pid_t child_pid;
int child_status;
char path[PATH_MAX+1];
char *progpath = strdup(args[0]);
char *prog = basename(progpath);
char temp[MAX_LINE];
}
它编译得很好,但是当我尝试运行代码时,它给了我分段错误错误
我该如何解决它以及为什么会出现此错误?
【问题讨论】:
-
试过初始化
args[0](定义)? -
char *progpath = strdup(args[0]);:args[0]未初始化。 -
这里的args[0]是什么
-
编译所有警告和调试信息 (
gcc -Wall -Wextra -g)。然后使用调试器 (gdb)(还有valgrind....)
标签: c linux segmentation-fault