简体   繁体   中英

How to fix 'Segmentation Fault (core dumped)' error

I am just starting to learn some C so I wrote a small program to exercise on using char arrays. It takes in a string from stdin and prints it after removing trailing whitespaces and empty lines (if any of you have the second edition of 'The C programming language' it's problem 1-18). After hitting the EOF key to end input, however, the program exits with error: 'Segmentation fault (core dumped)'.

I've already compiled with Wall , Wextra , Werror and g but no errors are displayed at compile time (using gcc 9.1.1 on Fedora 30). I also ran the program through gdb and discovered that the line that causes the fault is this line:

for (; (c = input[start + i]) != '\n' || c != '\0'; ++i)

I'm pasting the whole file since it's a short program and everything could be the cause of the error.

#include <stdio.h>

#define MAX 100

int Read(char input[], int max);
void ProcessInput(char input[], char output[], int length);
int GetToEndLine(char input[], char outLine[], int startIndex);

int main(void){
    char input[MAX];
    char output[MAX];
    int length;
    printf("W: Max output is of %d chars.\n", MAX - 1);
    length = Read(input, MAX);
    ProcessInput(input, output, length);
    printf("\nCleaned input:\n---\n%s\n---\n", output);
    return 0;
}

int Read(char input[], int max){
    int i, c;
    for (i = 0; (c = getchar()) != EOF && i < max - 1; ++i)
        input[i] = c;
    input[i] = '\0';
    return i;
}

void ProcessInput(char input[], char output[], int length){
    int mainIndex = 0,
        new_mainIndex = 0,
        outputIndex = 0;
    char line[MAX];
    while ((length - mainIndex) > 0){
        new_mainIndex = GetToEndLine(input, line, mainIndex);
        if (new_mainIndex == mainIndex){
            ++mainIndex;
            continue;
        }
        for (int j = new_mainIndex - mainIndex; 
                line[j] == ' ' || line[j] == '\t' || line[j] != '\n'; --j)
            line[j] = '\0';
        for(int j = 0; line[j] != '\0'; ++j, ++outputIndex)
            output[outputIndex] = line[j];
        output[outputIndex] = '\n';
        ++outputIndex;
        mainIndex = new_mainIndex + 1;
    }
}

int GetToEndLine(char input[], char line[], int start){
    int c, 
        i = 0;
    for (; (c = input[start + i]) != '\n' || c != '\0'; ++i)
        line[i] = c;
    line[i] = c;
    return start + i;
}

The full output of a successful test run should be:

W: Max output is of 99 chars.
asdf   

asdf
Cleaned Input:
---
asdf
asdf
---

Instead of that I get:

W: Max output is of 99 chars.
asdf   

asdfSegmentation fault(core dumped)

Can somebody help me debug this program?

It appears that GetToEndLine() will continue looping until it runs out of memory. Did you mean to use a logical and instead of a logical or? Try for (; (c = input[start + i]) != '\\n' && c != '\\0'; ++i)

int GetToEndLine(char input[], char line[], int start) {
    int c,
        i = 0;
    for (; (c = input[start + i]) != '\n' && c != '\0'; ++i)
        line[i] = c;
    line[i] = c; // What is the purpose of this?
    return start + i;
}

That should fix your immediate segfault problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM