0

I want to write a program which removes any word (user can type this word) from the string. Program works but valgirnd gives me some error messages:

==3009== Command: ./remove3 infile
==3009== 
Type word that you want to remove from the file:
go
==3009== Invalid write of size 1
==3009==    at 0x4009CD: main (remove3.c:74)
==3009==  Address 0x51f1041 is 0 bytes after a block of size 1 alloc'd
==3009==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3009==    by 0x400947: main (remove3.c:45)
==3009== 
==3009== Invalid read of size 1
==3009==    at 0x400AE6: DELTEword (remove3.c:115)
==3009==    by 0x400A39: main (remove3.c:90)
==3009==  Address 0x51f1041 is 0 bytes after a block of size 1 alloc'd
==3009==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3009==    by 0x400947: main (remove3.c:45)
==3009== 
==3009== Invalid read of size 1
==3009==    at 0x400B16: DELTEword (remove3.c:118)
==3009==    by 0x400A39: main (remove3.c:90)
==3009==  Address 0x51f1041 is 0 bytes after a block of size 1 alloc'd
==3009==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3009==    by 0x400947: main (remove3.c:45)
==3009== 
That  Katharina and Petruchio should be married,
And yet we hear not of our son-in-law.
What will  be said? what mockery will it be,
To what bridegroom when the priest attends 
To speak the  ceremonial rites of marriage!
What says Lucentio to this shame of ours?
 ==3009== 
==3009== HEAP SUMMARY:
==3009==     in use at exit: 1 bytes in 1 blocks
==3009==   total heap usage: 50 allocs, 49 frees, 1,181 bytes allocated
==3009== 
==3009== LEAK SUMMARY:
==3009==    definitely lost: 1 bytes in 1 blocks
==3009==    indirectly lost: 0 bytes in 0 blocks
==3009==      possibly lost: 0 bytes in 0 blocks
==3009==    still reachable: 0 bytes in 0 blocks
==3009==         suppressed: 0 bytes in 0 blocks
==3009== Rerun with --leak-check=full to see details of leaked memory
==3009== 
==3009== For counts of detected and suppressed errors, rerun with: -v
==3009== ERROR SUMMARY: 95 errors from 3 contexts (suppressed: 2 from 2)

also I want to ask about main body. I am not fully sure if the way I am getting characters from the keyboard is correct for instance: when I type "go" everything is okay but word "To" cannot be removed, why?

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define CHUNK 12

char *getWord(FILE *infile);
int DELTEword(char *word, char *KEYword);

char *getWord(FILE *infile)
{
    int length, cursor = 0, c;
    char *word, *word2;

    word = (char*)malloc(sizeof(char)*CHUNK);
    if(word == NULL) return NULL;

    length = CHUNK;

        while((c = getc(infile)) != ' ' && !feof(infile))
        {
            word[cursor] = c;
            cursor++;

            if(cursor >= length)
            {
                length += CHUNK;
                word2 = (char*)realloc(word, length*sizeof(char));
                if(word2 == NULL)
                {
                    free(word);
                    return NULL;
                }
                else word = word2;
            }
        }

    word[cursor] = '\0';
    return word;
}

int main(int argc, char *argv[])
{
    char *word, c, *keyWord = (char*)malloc(sizeof(char));
    FILE *infile;
    int length;
    int size = 20;

    if(argc != 2)
    {
        printf("\nMissing arguments.\n");
        abort();
    }

    if(keyWord == NULL) return 0;

    printf("Type word that you want to remove from the file:\n");

    length = 0;

    while(1)
    {
        if(length == size)
        {
            keyWord = (char*)realloc(keyWord, size + 10);
            size += 10;
        }

        c = getchar();

        if(c == '\n') break;

        keyWord[length] = c;
        length++;
    }

    infile = fopen(argv[1], "r");
    if(infile != NULL)
    {
        while(!feof(infile))
        {
            word = getWord(infile);
            if(word == NULL)
            {
                free(word);
                break;
            }

            if(DELTEword(word, keyWord) == 1)
            {
                printf(word, infile);
                printf(" ");
                free(word);
            }
        }
    }
    else
    {
        printf("It is impossible to open the infile\n");
        abort();
    }

    fclose(infile);
    return 0;
}

int DELTEword(char *word, char *KEYword)
{
    int i, k = 0, l = 0, length;
    char *ptr;

    if(word != NULL)
    {
        length = strlen(KEYword);
        for(i = 0; word[i] != '\0'; i++)
        {
            if(word[i] == KEYword[k])
            {
                l++;
                k++;
            }
            else break;

            if(l == length)
            {
                ptr = &word[i];
                memmove((ptr - length) + 1, ptr + 1, strlen((ptr - length) + 1));
                l = 0;
                k = 0;
            }
        }
        return 1;
    }
    else return 0;
 }

Thank you for any help.

1 Answer 1

5

Referring the invalid writes/reads logged by Valgrind:

You allocate 1 byte and set size to 20:

char *word, c, *keyWord = (char*)malloc(sizeof(char));
...
int size = 20;

This should somehow be linked, for example like this:

int size = 20;
char *word, c, *keyWord = malloc(size * sizeof(*keyWord));

Also it seems you are missing to allocate room for the "string"'s 0-terminator. In C "strings" are character array which have their last valid character indicated by a following NUL character.

So you always need to allocate one more then the maximum number of characters the "string" shall be able to hold.


Btw: In C there is no need to cast malloc/calloc/realloc, nor is it recommended: https://stackoverflow.com/a/605858/694576

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.