简体   繁体   中英

Segmentation Fault when finding longest word in input

I have written a program to find the longest word in the input. I get no errors when using valgrind or running tests locally, but the grading program I email the code to reports a segmentation fault.

int main(void)
{
char *longest = malloc(1);
size_t size = 1;
do {
    char word[20];
    if (scanf("%s", word) > 0) {
        if (strlen(word) > size) {
        longest = realloc(longest,strlen(word)+1);
        strcpy(longest,word);
        size = strlen(word);
        }
    }
} while (getchar() != EOF);
printf("%zu characters in longest word: %s\n", strlen(longest),longest);
free(longest);
return 0;
}

Your problem is in the line char word[20]; and the way scanf reads words. From scanf 's point of view, a word is any sequence of non-spaces. For example, realloc(longest,strlen(word)+1); is treated as one word, and that alone is longer than 20 characters.

You should use a more robust function to read words and allocate space for them. The most cost-efficient solution is getline() for reading the line followed by strsep() for extracting words.

Without relying on the luxurities of POSIX-functions, only standard-C for variable word-length:

#include <assert.h>   // assert()
#include <stddef.h>   // size_t
#include <stdbool.h>  // bool, true, false
#include <stdlib.h>   // EXIT_FAILURE, realloc(), free()
#include <stdio.h>    // fscanf(), fgetc(), ungetc(), printf(), fputs()
#include <ctype.h>    // isspace()
#include <string.h>   // strlen(), strcat(), strcpy()

#define WORD_BUFFER_SIZE 20

#define STRING(value) #value
#define STRINGIFY(value) STRING(value)

// reads and discards whitespace, returns false on EOF
bool skip_ws(FILE *stream)
{
    int ch;
    while ((ch = fgetc(stream)) != EOF && isspace(ch));
    if(!isspace(ch) && ch != EOF)  // if it was not whitespace and not EOF
        ungetc(ch, stream);        // pretend we were never here.
    return ch != EOF;
}

bool read_word(char **dst, FILE *stream)
{
    assert(dst);
    char chunk_buffer[WORD_BUFFER_SIZE + 1];

    if (!skip_ws(stream))  // if we encounter EOF before any other non-whitespace
        return false;

    // read chunk by chunk
    for (size_t i = 0; fscanf(stream, "%" STRINGIFY(WORD_BUFFER_SIZE) "s", chunk_buffer) == 1; ++i)
    {
        size_t chunk_length = strlen(chunk_buffer);
        // adjust *dst's size
        char *tmp = realloc(*dst, (i * WORD_BUFFER_SIZE + chunk_length + 1) * sizeof(*tmp));
        if (!tmp) {
            free(*dst);
            *dst = NULL;
            return false;
        }
        *dst = tmp;
        if (i == 0)         // zero terminate it if it is the first junk
            **dst = '\0';  // for strcat() to behave well.
        strcat(*dst, chunk_buffer);  // add the current chunk to *dst.

        int next_char = fgetc(stream);
        ungetc(next_char, stream);

        if (chunk_length < WORD_BUFFER_SIZE || isspace(next_char) || next_char == EOF)
            return true;
    }

    return true;
}

int main(void)
{
    char *word = NULL;
    char *longest_word = NULL;
    size_t longest_length = 0;

    while (read_word(&word, stdin)) {
        size_t length = strlen(word);
        if (length > longest_length) {
            char *tmp = realloc(longest_word, (length + 1) * sizeof *tmp);
            if (!tmp) {
                fputs("Not enough memory. :(\n\n", stderr);
                free(longest_word);
                free(word);
                return EXIT_FAILURE;
            }
            longest_length = length;
            longest_word = tmp;
            strcpy(longest_word, word);
        }       
    }
    free(word);

    printf("%zu characters in the longest word: \"%s\"\n\n", longest_length, longest_word);
    free(longest_word);
}

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