usuwanie slowa z pliku tekstowego (wycieki pamieci)

0

Napisalem program, ktory powinien usuwac slowo podane przez uzytkownika. Program dziala prawie dobrze tzn. usuwa slowo "go" ale slowa "To" juz nie. Dlaczego? Poza tym valgrind caly czas wywala mi bledy. Czesc juz poprawilem a reszty nie moge znalezc.

Valgrind:

==5328== Memcheck, a memory error detector
==5328== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==5328== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==5328== Command: ./remove3 infile
==5328== 
Type word that you want to remove from the file:
go
==5328== Conditional jump or move depends on uninitialised value(s)
==5328==    at 0x400B61: DELTEword (remove3.c:123)
==5328==    by 0x400A9E: main (remove3.c:96)
==5328== 
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?
 ==5328== 
==5328== HEAP SUMMARY:
==5328==     in use at exit: 0 bytes in 0 blocks
==5328==   total heap usage: 50 allocs, 50 frees, 1,200 bytes allocated
==5328== 
==5328== All heap blocks were freed -- no leaks are possible
==5328== 
==5328== For counts of detected and suppressed errors, rerun with: -v
==5328== Use --track-origins=yes to see where uninitialised values come from
==5328== ERROR SUMMARY: 45 errors from 1 contexts (suppressed: 2 from 2)

oraz moj kod programu:

#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[])
{
	FILE *infile;
	int length;
	int size = 20;
	char *word, c, *keyWord, *keyWord2;
	
	if(argc != 2)
	{
		printf("\nMissing arguments.\n");
		abort();
	}
	
	keyWord = malloc(size*sizeof(*keyWord));
	if(keyWord == NULL) exit(EXIT_FAILURE);
	
	printf("Type word that you want to remove from the file:\n");

	length = 0;
	
	while(1)
	{
		if(length >= size)
		{
			size += CHUNK;
			keyWord2 = realloc(keyWord, size*sizeof(char));
			if(keyWord2 == NULL)
			{
				free(keyWord);
				exit(EXIT_FAILURE);
			}
			else keyWord = keyWord2;
		}
		
		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");
		free(keyWord);
		abort();
	}
	
	free(keyWord);
	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;
 }

Dziekuje za wszelka pomoc.

dodanie znacznika <code> - fp

3

Skąd ci się wzięły te „wycieki pamięci”, skoro twój problem polega na błędnym działaniu algorytmu, a valgrind pisze “All heap blocks were freed -- no leaks are possible”

Poza tym valgrind caly czas wywala mi bledy
Dostałeś dokładne miejsce błędu:

==5328== Conditional jump or move depends on uninitialised value(s)
==5328==    at 0x400B61: DELTEword (remove3.c:123)
==5328==    by 0x400A9E: main (remove3.c:96)

Tłumaczę: Skok lub zapis warunkowy zależny od niezaincjalizowanej wartości.

0

zmienilem troche w main'ie i chyba teraz wszystko gra poza jednym: nadal niektorych slow nie moge usunac. Pomoze ktos z funkcja usuwajaca slowa.

Valgrind:

==6746== Memcheck, a memory error detector
==6746== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6746== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6746== Command: ./remove3 infile
==6746== 
Type word that you want to remove from the file:
go
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?
 ==6746== 
==6746== HEAP SUMMARY:
==6746==     in use at exit: 0 bytes in 0 blocks
==6746==   total heap usage: 50 allocs, 50 frees, 1,192 bytes allocated
==6746== 
==6746== All heap blocks were freed -- no leaks are possible
==6746== 
==6746== For counts of detected and suppressed errors, rerun with: -v
==6746== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

Zmieniony main:

int main(int argc, char *argv[])
{
	FILE *infile;
	char *word;
	char keyWord[CHUNK], *pointer;
	
	if(argc != 2)
	{
		printf("\nMissing arguments.\n");
		abort();
	}
	
	printf("Type word that you want to remove from the file:\n");
	gets(keyWord);
	
        pointer = keyWord;
	pointer = (char*)malloc(sizeof(char)*(CHUNK+1));
	
	if(pointer == NULL)
	{
		printf("Unable to allocate memory!\n");
		exit(EXIT_FAILURE);
	}

	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");
		free(pointer);
		abort();
	}
	
	free(pointer);
	fclose(infile);
	return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1