【问题标题】:I'm at a loss at finding the segfault in my program我在我的程序中找不到段错误
【发布时间】:2023-03-20 06:24:01
【问题描述】:

所以我一直在花费数小时试图找到这个分段错误 11 错误的来源,我希望你们能帮助我。

所以程序的目的是在输入字符串中找到重定向,并返回一个 redirCode 结构,该结构给出重定向的方向、文件名和 argsWithoutFile。

示例输入:

"ls -a > test.txt"

返回一个重定向代码:

argsWithoutFile = "ls -a"
fileName = "test.txt"
code = 2                     //stdout

我很确定 seg 错误来自尝试执行子字符串。 for 循环似乎很好,因为当我在最后注释掉子字符串的内容时,它不会给出段错误。除了子字符串,我觉得一切都很好。基本上对于子字符串,我希望它是这样的:

char *input = "ls -a > test.txt'
char *desiredSubString = "ls -a "

这里是完整的代码:

#include <stdio.h>
#include <string.h>
#define BUFFER      1024
struct redirCode findRedirects(char *input);
struct redirCode {
    /* For code:
     * 0 = none
     * 1 = stdin
     * 2 = stdout
     */
    int code;
    char *argsWithoutFile;
    char *fileName;
};

int main(){
    const char *delims = "<>";
    struct redirCode temp;
    char line[BUFFER];

    printf("Input: ");
    fgets(line, 1024, stdin);
    temp = findRedirects(line);

    printf("temp:\n");
    printf("temp.code = %d\n", temp.code);
    printf("temp.fileName = %s\n", temp.fileName);
    printf("temp.argsWithoutFile = %s\n", temp.argsWithoutFile);

}

/* Looks for '>', '<' in a string.
 * Will destroy string *input
 * Returns a redirCode struct with:
 * 1. fileName - the name of file that
 * wants to be redirected/stdin
 * 2. code - the direction of redirect
 * 3. args - the arguments w/o filename
 * */
struct redirCode findRedirects(char *input)
{
    const char *delims = "<>";
    struct redirCode redirToReturn;

    //Do an initial search for the delimeters
    //before strtok destroys it. O(n) time.
    int redirectOperatorReached = 0;
    int count = 0;

    int i;
    for (i = 0; input[i] != 0; i++){
        if (input[i] == '<'){
            redirToReturn.code = 1;
            redirectOperatorReached = 1;
        }
        else if (input[i] == '>'){
            redirToReturn.code = 2;
            redirectOperatorReached = 1;
        }
        else {
            redirToReturn.code = 0;
        }
        if (redirectOperatorReached != 1){
            count++;
        }
    }
    printf("sizeof(input) = %lu\n", sizeof(input));
    printf("count = %d\n", count);
    strncpy(redirToReturn.argsWithoutFile, input, count);
    printf("input = %s\n", input);
    redirToReturn.argsWithoutFile[count] = '\0';
    printf("argsW/oFile = %s\n", redirToReturn.argsWithoutFile);

    return redirToReturn;
}

这里有一些终端日志

MacBook-Air:practice keithy$ cc strtokOnlyOnce.c
MacBook-Air:practice keithy$ ./a.out
Input: hi
sizeof(input) = 8
count = 3
Segmentation fault: 11
MacBook-Air:practice keithy$ cc strtokOnlyOnce.c
MacBook-Air:practice keithy$ ./a.out
Input: ls -a > test.txt
sizeof(input) = 8
count = 6
Segmentation fault: 11
MacBook-Air:practice keithy$

编辑:我让它工作了!我所要做的就是在redirCode 中malloc 字符串。这是工作代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER      1024
struct redirCode findRedirects(char *input);
struct redirCode {
    /* For code:
     * 0 = none
     * 1 = stdin
     * 2 = stdout
     */
    int code;
    char *argsWithoutFile;
    char *fileName;
};

int main(){
    const char *delims = "<>";
    struct redirCode temp;
    char line[BUFFER];

    printf("Input: ");
    fgets(line, 1024, stdin);
    temp = findRedirects(line);

    printf("temp.code = %d\n", temp.code);
    printf("temp.fileName = %s\n", temp.fileName);
    printf("temp.argsWithoutFile = %s\n", temp.argsWithoutFile);

}

/* Looks for '>', '<' in a string.
 * Will destroy string *input
 * Returns a redirCode struct with:
 * 1. fileName - the name of file that
 * wants to be redirected/stdin
 * 2. code - the direction of redirect
 * 3. args - the arguments w/o filename
 * */
struct redirCode findRedirects(char *input)
{
    const char *delims = "<>";
    struct redirCode *redirToReturn = malloc(sizeof(struct redirCode));

    //Do an initial search for the delimeters
    //before strtok destroys it. O(n) time.
    int redirectOperatorReached = 0;
    int count = 0;

    int i;
    for (i = 0; input[i] != 0; i++){
        if (input[i] == '<'){
            redirToReturn->code = 1;
            redirectOperatorReached = 1;
            input[i] = ' ';
        }
        else if (input[i] == '>'){
            redirToReturn->code = 2;
            redirectOperatorReached = 1;
            input[i] = ' ';
        }
        if (redirectOperatorReached != 1){
            count++;
        }
    }
    int lengthOfInput = strlen(input);

    int sizeOfMalloc = (lengthOfInput+1)*sizeof(char);
    redirToReturn->argsWithoutFile = (char *) malloc(sizeOfMalloc);
    redirToReturn->fileName = (char *) malloc(sizeOfMalloc);

    strncpy(redirToReturn->argsWithoutFile, input, count);
    redirToReturn->argsWithoutFile[count] = '\0';

    strncpy(redirToReturn->fileName, input + count, lengthOfInput - count);

    return *redirToReturn;
}

/*OUTPUT
 *./a.out
 *Input: ls -a > test.txt
 *temp.code = 2
 *temp.fileName =   test.txt
 *temp.argsWithoutFile = ls -a
*/

【问题讨论】:

  • 您是否尝试过使用调试器?如果你用你的程序运行一个调试器,那么它会准确地告诉你在程序中的哪一点发生了段错误。
  • argsWithoutFile 为 NULL(好吧,用随机值初始化)。您首先必须分配一个缓冲区,然后才能将其用于 strncpy 。
  • 您在 printf 调试方面做得非常好,但您需要进行下一步。输出将strncpy(redirToReturn.argsWithoutFile, input, count); 标识为问题行。所以,深入挖掘那里。了解这个特定程序的问题并不是真正的大问题。这是您学习一些调试技能的机会,这些技能将为您服务。
  • 您的for 循环看起来好像会在扫描文件名时将代码重置为零。此外,命令可以重定向输入和输出,但这可能是未来的增强。
  • 请注意,sizeof 正在返回 8,无论它为您提供 char * 指针 的大小而不是 数据它指向的那个。

标签: c


【解决方案1】:
  struct redirCode {
        /* For code:
         * 0 = none
         * 1 = stdin
         * 2 = stdout
         */
        int code;
        char *argsWithoutFile;
        char *fileName;
    };

使用前需要为名为@9​​87654322@和char *fileName;的结构成员分配内存。

你可以这样做

struct redirCode *redirToReturn = malloc(sizeof(struct redirCode ));
redirToReturn->argsWithoutFile = malloc(1024);
redirToReturn->fileName = malloc(1024);

【讨论】:

    【解决方案2】:

    1-> 你需要分配内存给argsWithoutFile 在做strncpy之前使用new/malloc 2-> 文件名未初始化,您正在 printf 中打印它。这是未定义的行为。

    【讨论】:

      猜你喜欢
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      相关资源
      最近更新 更多