Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
problem with freeing data
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="david, post: 3466042"] 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- 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. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
problem with freeing data
Top