memory leak

This is a discussion on memory leak within the C Programming forums, part of the General Programming Boards category; i wrote this function to scan a word from a file: char * fgetword(FILE *f) { char *word,*tmp,c; int i ...

  1. #1
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90

    memory leak

    i wrote this function to scan a word from a file:

    char * fgetword(FILE *f)
    {
    char *word,*tmp,c;
    int i = 0;


    word = (char*)calloc(MAXLENGHT,sizeof(char));
    tmp = word;
    while((c = fgetc(f)) <=32 && c!= EOF)
    ;
    if (c == EOF)
    {
    free(word);
    word = NULL;
    }
    else
    *tmp++ = c;
    while((c = fgetc(f)) > 32 && c != EOF && i++ < MAXLENGHT)
    *tmp++ = c;
    *tmp = '\0';
    return word;
    }


    i using it to parse a file with hundredthousands lines of text. i use it like this:

    while ((word = fgetword(f1)) != NULL && strcmp(word,"Testfallname:") != 0 )
    {;}
    if (word == NULL)
    return 0;

    fputs("Testname: ",f2);

    free(word);


    this whole thing is in one big while not eof loop.

    after scanning and working correct for about 5 to 10 minutes, it crashes.

    is there a fault in my free()? is there any good way of detecting memory leaks....?

  2. #2
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    ok, first problem is solved. i oversaw a missing free (stupid i know).

    but how can i manage to allocate exactly as much memory for my word as it needs? (if not knowing how long this word will gona be) I thought about resizing the allocated memory everytime i read a char. but how could i do that?

    stormbringer

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I thought about resizing the allocated memory everytime i read a char. but how could i do that?
    realloc is your friend.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    ok, but i still have one problem. if i reallocate memory i need to know how big my vector of (char) memory already ist. i could use a integer to count how many chars large it is already and do

    myptr = (char*)realloc(myptr,(++i)*sizeof(char));

    But because i have very large files with a lot of crap like %*%"..., there is always the possibilyti of an overflow of the i variable.

    i have a initial pointer p to the begining of the block of memory and myptr is "running" over it. something like:
    p = (char*)malloc(sizeof(cahr));

    myptr = p;
    while(c != ' ')
    {
    *myptr = fgetc(f);
    c = *tmp;
    if ((buf = (char *)realloc(buf,(sizeof(*buf) + sizeof(char)))) == NULL)
    return NULL;
    tmp++;
    }
    *tmp = '\0';
    return buf;

    as you see, this won't work, because sizeof(*buf) returns the value of a single char.

    i also tried : buf = (char *)realloc(buf,((&(*myptr) - &(*p)) + sizeof(char))

    but when the memory block is too big, realloc moves it position and &(*myptr)-&(*p) neither is the right size nor is p pointing to the beginning of the "string". why? (if i count the size of &(*myptr)-&(*p) it is during the loops: 1,2,3,4,5,6,7,8,9,6474)

    so how can i decide the lenght of the memory already allocated?

    stormbringer
    Last edited by stormbringer; 07-17-2002 at 01:38 AM.

  5. #5
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    i solved the problem so far, but it ocures a strange error. due to topic change i will reopen a new thread called: strange memory error.

    thanks for helping

    stormbringer

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    You must keep track of that yourself.

    Also, (1) you cannot free memory and then return it from a function as a valid pointer! (2) You are wasting valuable resources/time by requesting/unrequesting chunks of data like that.
    I have a suggestion: Use a "static" buffer that is created only once and return a pointer to the current position.
    Code:
    #include <ip.hpp>

  7. #7
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    1) i am not freeing the alocated memory, so returning a pointer to the starting adress is valid. i just must take care to free it later.

    2) this buffer ist sort of static, because it's only allocated once per function call. the loop just resizes the buffer.

    3) i am trying not to waiste resources because i just allocate as much memory as i use, no ie 1000 of char.

    anyway, i solved that but i must agree the code here ist sloppy and not very ressource friendly

    thanks

    storm

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ok, but i still have one problem. if i reallocate memory i need to know how big my vector of (char) memory already ist. i could use a integer to count how many chars large it is already and do
    That's why you have nice little functions called 'strlen'.

    i = strlen( str );
    ptr = realloc( str, i + 2 );
    if( ptr != NULL )
    {
    ptr[i++] = newChar;
    ptr[i] = '\0';
    }
    else
    {
    printf("Dooooom!\n");
    exit( 0 );
    }

    Put all that in a loop, with a statement which pulls one character from the iput stream and sticks it in 'newChar', and you're all set.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 05:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21