problem with freeing data

Discussion in 'C Programming' started by david, Mar 4, 2008.

  1. david

    david Guest

    I will past only two segments from the code it should be enough to see
    what I did wrong, I think I know there I made a mistake, but how to
    fix it I can not tell. This why I need help from you all.

    Main code:
    ----------------
    /* duomenu rasymas i faila */
    dst = fopen(argv[2], "w");
    if (dst != NULL) {
    wordDBSize = sizeStack(wordDB);
    for (tmp = 0; tmp < wordDBSize; tmp++) {
    word = NULL;
    word = popStack(wordDB);
    if (fprintf(dst, "%d: %s\n", tmp + 1, word) < 0)
    show_err("(antra)[main] Can not write to file. Please
    check <destination_file> permissions. Exiting.");
    /* FIXME - PROBLEM HERE - (line below) */
    free(word);
    }
    charAverage = wordCountAll / (float)wordCount;
    fprintf(dst, "Average (Words: %d, Total chars: %d) : %.2f\n",
    wordCount, wordCountAll, charAverage);
    fclose(dst);
    destroyStack(&wordDB);
    } else
    show_err("(antra)[main] Can not create <destination_file>
    file.");

    Stack code:
    -----------------
    char* popStack(stack *item) {
    child *tmp;
    char *value;

    if (item->root == NULL)
    return NULL;
    value = item->root->value;
    tmp = item->root;
    item->root = item->root->next;
    free(item->root);
    item->size--;

    return value;
    }

    I highlighted in the code there I am have problem. As you can see when
    I call popStack it return pointer to the string, which is in my linked
    list element and then I free memory of the element. free(word);
    returns error:

    Macbook:antra_new marius$ ./antra src.txt dst.txt sep.txt
    antra(882) malloc: *** error for object 0xc0000003: Non-aligned
    pointer being freed
    *** set a breakpoint in malloc_error_break to debug
    Segmentation fault

    So, I think that freeing the element of my list frees my string, which
    pointer is located in that element. Removing "free(item-
    >root);" (stack code) fix this problem and now I can free my string in

    the main code, but as you can see in this situation I am leaving
    garbage in the memory and I don not want to do that.

    Firstly I would like to know, does freeing my element from linked list
    frees the string which pointer is located in that element. And finally
    how could I fix that?

    P.S. My stack holds the pointers in char arrays.
    david, Mar 4, 2008
    #1
    1. Advertising

  2. david

    david Guest

    P.S. My stack holds the pointers to(in) char arrays
    david, Mar 4, 2008
    #2
    1. Advertising

  3. david

    Mark Bluemel Guest

    david wrote:
    [Snip]

    > char* popStack(stack *item) {
    > child *tmp;
    > char *value;
    >
    > if (item->root == NULL)
    > return NULL;
    > value = item->root->value;
    > tmp = item->root;
    > item->root = item->root->next;
    > free(item->root);


    Are you _sure_ you meant to do this, rather than free(tmp)?
    Mark Bluemel, Mar 4, 2008
    #3
  4. david

    Mark Bluemel Guest

    david wrote:
    > I will past only two segments from the code it should be enough to see
    > what I did wrong,


    No. It isn't enough.

    As you don't know what's wrong, I don't see how you think you can decide
    how much we need to know...

    In future, please post something complete enough for us to see what's
    happening - ideally produce the smallest possible self-contained
    testcase.

    [snip]

    > free(word);


    Was "word" allocated with malloc/calloc/realloc? We don't know, because
    you haven't shown us....

    [Snip]

    > Firstly I would like to know, does freeing my element from linked list
    > frees the string which pointer is located in that element. And finally
    > how could I fix that?


    free(pointer) frees what the pointer points to, no more and no less.
    (Obviously "pointer" must point to space allocated by one of the
    malloc family of functions).

    > P.S. My stack holds the pointers in char arrays.


    What is that supposed to mean?
    Mark Bluemel, Mar 4, 2008
    #4
  5. david

    Richard Bos Guest

    david <> wrote:

    > word = popStack(wordDB);


    > /* FIXME - PROBLEM HERE - (line below) */
    > free(word);


    > char* popStack(stack *item) {


    > value = item->root->value;
    > tmp = item->root;
    > item->root = item->root->next;
    > free(item->root);


    This is wrong; you want free(tmp); here. But that's probably not what's
    causing your problem.

    > return value;
    > }


    > antra(882) malloc: *** error for object 0xc0000003: Non-aligned
    > pointer being freed


    > So, I think that freeing the element of my list frees my string,


    That depends. If item->root->value is a char *, it doesn't. If it's a
    char[], it does. But you have failed to show us two critical parts of
    your code:
    - the definition of a stack, and that of stack's member root;
    - the code where you put your words _in_ your stack, in particular, how
    you declare the memory for each root's value string.

    If you malloc() memory for each word separately before you push them
    onto your stack, and don't manipulate that address before you push it on
    (or, say, free() it in between), you should be able to free() it. OTOH,
    if you get the memory for each word from part of a single malloc(), or
    from an automatic ("local") variable, or any number of other
    possibilities, you not only do not need to free() it, but shouldn't.

    What the solution to your problem is depends on the two bits of code I
    mentioned above.

    Richard
    Richard Bos, Mar 4, 2008
    #5
  6. david

    david Guest

    The is not that small and I will post in the one of pastebin websites.

    stack.h - http://www.paste.lt/paste/6aa50aa1d92f58faaf56c8d24e3f1f16
    stack.c - http://www.paste.lt/paste/684c6fa2ef499d9fab2d4be11c65e796
    antra.c (main) - http://www.paste.lt/paste/0f2dd328b28e561a6c0383ad9fb41cdf

    antra_lib.c - http://www.paste.lt/paste/83ec85cface8e1215055f7acc4d106f0
    antra_lib.h - http://www.paste.lt/paste/cc88b47394767fc242edd6edf9487b4c

    I am sorry that I am not using buffered reading in this program. This
    is the hole program and you should be able to compile it.
    ../antra <source_file> <destination_file> <separators_list_file>
    should put words in destination file whose length is even (sorry if
    wrong word, my native language is not English) and word does not
    contains any number.

    I use realloc to allocate memory and grow my string until I get to
    separator and when I push to stack the pointer of this string and then
    I NULL the pointer and reseting the length of string back to zero,
    this should protect the later string I created. And again I reallocate
    a new one and etc. till the end.

    Stack struct has pointer "root" which points to the top of my stack.
    child struct contains of next pointer (next item in the linked list)
    and char pointer there it holds the location of the string I pushed.

    I again read how works realloc and I can see that I should be using it
    correctly, if pointer is NULL when realloc works just like malloc.

    I am very interested why I can not free that string later after poping
    it from stack.
    Thanks everyone for helping.
    david, Mar 4, 2008
    #6
  7. david <> writes:

    > The is not that small and I will post in the one of pastebin websites.
    >
    > stack.h - http://www.paste.lt/paste/6aa50aa1d92f58faaf56c8d24e3f1f16
    > stack.c - http://www.paste.lt/paste/684c6fa2ef499d9fab2d4be11c65e796
    > antra.c (main) - http://www.paste.lt/paste/0f2dd328b28e561a6c0383ad9fb41cdf
    >
    > antra_lib.c - http://www.paste.lt/paste/83ec85cface8e1215055f7acc4d106f0
    > antra_lib.h -
    > http://www.paste.lt/paste/cc88b47394767fc242edd6edf9487b4c


    This looks like the code you posted earlier. The popStack function
    still has the error that I saw being pointer out so I don't see any
    point in taking time to look at the rest. You are freeing the wrong
    thing and you need to fix that before anything else.

    --
    Ben.
    Ben Bacarisse, Mar 4, 2008
    #7
  8. david

    Guest

    In this code:
    1 for (tmp = 0; tmp < wordDBSize; tmp++) {
    2 word = NULL;
    3 word = popStack(wordDB);
    4 if (fprintf(dst, "%d: %s\n", tmp + 1, word) < 0)
    5 show_err("(antra)[main] Can not write to file. Please
    check <destination_file> permissions. Exiting.");
    6 /* FIXME - PROBLEM HERE - (line below) */
    7 free(word);
    8 }

    In line 3, you get "word" from popStack(). What if word was NULL?
    popStack() do return NULL on one case. You can't free NULL


    david wrote:
    > I will past only two segments from the code it should be enough to see
    > what I did wrong, I think I know there I made a mistake, but how to
    > fix it I can not tell. This why I need help from you all.
    >
    > Main code:
    > ----------------
    > /* duomenu rasymas i faila */
    > dst = fopen(argv[2], "w");
    > if (dst != NULL) {
    > wordDBSize = sizeStack(wordDB);
    > for (tmp = 0; tmp < wordDBSize; tmp++) {
    > word = NULL;
    > word = popStack(wordDB);
    > if (fprintf(dst, "%d: %s\n", tmp + 1, word) < 0)
    > show_err("(antra)[main] Can not write to file. Please
    > check <destination_file> permissions. Exiting.");
    > /* FIXME - PROBLEM HERE - (line below) */
    > free(word);
    > }
    > charAverage = wordCountAll / (float)wordCount;
    > fprintf(dst, "Average (Words: %d, Total chars: %d) : %.2f\n",
    > wordCount, wordCountAll, charAverage);
    > fclose(dst);
    > destroyStack(&wordDB);
    > } else
    > show_err("(antra)[main] Can not create <destination_file>
    > file.");
    >
    > Stack code:
    > -----------------
    > char* popStack(stack *item) {
    > child *tmp;
    > char *value;
    >
    > if (item->root == NULL)
    > return NULL;
    > value = item->root->value;
    > tmp = item->root;
    > item->root = item->root->next;
    > free(item->root);
    > item->size--;
    >
    > return value;
    > }
    >
    > I highlighted in the code there I am have problem. As you can see when
    > I call popStack it return pointer to the string, which is in my linked
    > list element and then I free memory of the element. free(word);
    > returns error:
    >
    > Macbook:antra_new marius$ ./antra src.txt dst.txt sep.txt
    > antra(882) malloc: *** error for object 0xc0000003: Non-aligned
    > pointer being freed
    > *** set a breakpoint in malloc_error_break to debug
    > Segmentation fault
    >
    > So, I think that freeing the element of my list frees my string, which
    > pointer is located in that element. Removing "free(item-
    > >root);" (stack code) fix this problem and now I can free my string in

    > the main code, but as you can see in this situation I am leaving
    > garbage in the memory and I don not want to do that.
    >
    > Firstly I would like to know, does freeing my element from linked list
    > frees the string which pointer is located in that element. And finally
    > how could I fix that?
    >
    > P.S. My stack holds the pointers in char arrays.
    , Mar 5, 2008
    #8
  9. david

    david Guest

    Thought about that and checked some time ago, it does not return NULL;
    It does return exact the same amount of strings (char pointers) I
    pushed in the code above, the only problem it does not show the words
    and some garbage.

    Ben:
    Sorry, only after your second post I noticed the mistake. I will try
    to recompile code when I have a chance.
    david, Mar 5, 2008
    #9
  10. writes:
    [...]
    > In line 3, you get "word" from popStack(). What if word was NULL?
    > popStack() do return NULL on one case. You can't free NULL

    [...]

    Yes, you can. free(NULL) does nothing. (Doing so may well be an
    error; I haven't looked closely at the code.)

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 5, 2008
    #10
  11. david

    Mark Bluemel Guest

    david wrote:
    > The
    Code:
     is not that small and I will post in the one of pastebin websites.[/color]
    
    Why? Why not do the work to cut your problem down to a manageable size -
    the smallest program which demonstrates your problem? In doing so, you'd
    very likely find the problem for yourself.
    
    Some (probably many) of us will not bother to go to websites and trawl 
    through masses of code to debug your code for you for free. We will look
    closely at postings which show some effort has been expended.
    Mark Bluemel, Mar 5, 2008
    #11
  12. david

    santosh Guest

    Mark Bluemel wrote:

    > david wrote:
    >> The
    Code:
     is not that small and I will post in the one of pastebin
    >> websites.[/color]
    > 
    > Why? Why not do the work to cut your problem down to a manageable size
    > - the smallest program which demonstrates your problem? In doing so,
    > you'd very likely find the problem for yourself.
    > 
    > Some (probably many) of us will not bother to go to websites and trawl
    > through masses of code to debug your code for you for free. We will
    > look closely at postings which show some effort has been expended.[/color]
    
    In this case just running the files through lint spots the mistake,
    though I'll admit that it is hard to spot the main mistake from many
    other warnings and suspect conditions.
    santosh, Mar 5, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordon Beaton

    Freeing object created in JNI code?

    Gordon Beaton, Oct 27, 2003, in forum: Java
    Replies:
    1
    Views:
    319
    Tony Tang
    Oct 27, 2003
  2. Rajesh.Rapaka

    freeing memory

    Rajesh.Rapaka, Apr 20, 2005, in forum: Java
    Replies:
    17
    Views:
    2,826
    Eric Sosman
    Apr 21, 2005
  3. Replies:
    4
    Views:
    299
    Barry Schwarz
    May 13, 2007
  4. Jonathan de Boyne Pollard

    Re: Problem with freeing memory

    Jonathan de Boyne Pollard, Feb 18, 2010, in forum: C++
    Replies:
    20
    Views:
    654
    Marcel Müller
    Feb 19, 2010
  5. szimek
    Replies:
    0
    Views:
    113
    szimek
    Jun 22, 2009
Loading...

Share This Page