Linked List in C not working Valgrind Error -
the program supposed read words of file linked list. got linked list working i'm faced error. i'm getting segmentation fault when using large file.
it working smaller lists (3-10 words) not larger ones.
-----code----- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node //struct linked list { char* word; struct node* next; } node; int main (void) { char* dictfile = "large"; file* dict = fopen(dictfile, "r"); if (dict == null) return 1; node* head = null; char* oneword = malloc(sizeof(node)); //to store word fgets() while ((fscanf (dict, "%s", oneword)) > 0) { node* temp = (node*)malloc(sizeof(node)); char* tempword = (char*)malloc(sizeof(node)); //gives me new pointer store string (as pointed out antione) strcpy(tempword, oneword); temp->word = tempword; printf("%s\n", temp->word); //prints value (just debug) temp->next = head; head = temp; } node* temp = (node*)malloc(sizeof(node)); temp = head; while(temp != null) //loop print linked list { printf("traverse %s\n", temp->word); //prefix 'ing traverse know linked list temp = temp->next; } free(head); free(temp); fclose(dict); printf("success!!\n"); }
the result follows:
./tempeol .............lots of words below acceptor acceptor's tempeol: malloc.c:2372: sysmalloc: assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed. aborted (core dumped)
valgrind shows invalid writes @ few places.
one strcpy(). have account additional null character resolve don't know how.
-----valgrind error----- jharvard@appliance (~/dropbox/pset5): valgrind ./tempeol ==4709== memcheck, memory error detector ==4709== copyright (c) 2002-2013, , gnu gpl'd, julian seward et al. ==4709== using valgrind-3.10.0.svn , libvex; rerun -h copyright info ==4709== command: ./tempeol ==4709== aaa aaas aachen aalborg ==4709== invalid write of size 1 ==4709== @ 0x40db401: _io_vfscanf (vfscanf.c:1180) ==4709== 0x40e21f6: __isoc99_fscanf (isoc99_fscanf.c:34) ==4709== 0x8048681: main (tempeol.c:25) ==4709== address 0x423b1c0 0 bytes after block of size 8 alloc'd ==4709== @ 0x402a17c: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==4709== 0x8048662: main (tempeol.c:23) ==4709== ==4709== invalid read of size 1 ==4709== @ 0x402d4f1: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==4709== 0x80486d5: main (tempeol.c:31) ==4709== address 0x423b1c0 0 bytes after block of size 8 alloc'd ==4709== @ 0x402a17c: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==4709== 0x8048662: main (tempeol.c:23) ==4709== aalesund
help needed!!
also funny thing: if change char* tempword = (char*)malloc(sizeof(node));
char* tempword = (char*)malloc(sizeof(node)*512);
end getting full linked list segmentation fault @ end.
i feel solution lies in line of code. ideas??
===============================================================
thanks @drc , @lurker, i've got working. valgrind shows no errors. no segmentation faults. :)
here's final working code needs it:
================working code:: solution============ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node //struct linked list { char* word; struct node* next; } node; int main (void) { char* dictfile = "small"; file* dict = fopen(dictfile, "r"); if (dict == null) return 1; node* head = null; char oneword[28]; //to store word fscanf() while ((fscanf (dict, "%s", oneword)) > 0) { node* temp = (node*)malloc(sizeof(node)); char* tempword = (char*)malloc(strlen(oneword)+1); //gives me new pointer store string (as pointed out antione) strcpy(tempword, oneword); temp->word = tempword; //printf("%s\n", temp->word); //prints value (just debug) temp->next = head; head = temp; } node* temp = head; while(temp != null) //loop print linked list { printf("%s\n", temp->word); //prefix 'ing traverse know linked list temp = temp->next; } printf("\n"); free(head); //free(temp); //no need (drc) fclose(dict); printf("success!!\n"); }
i can sleep now...
you have correct line of code first problem. malloc memory hold string based on sizeof(node). instead, should of length strlen(oneword)+1
. right now, long word write beyond buffer.
char* tempword = (char*)malloc(strlen(oneword)+1);
in lines:
...} node* temp = (node*)malloc(sizeof(node)); temp = head; while(temp != null) ...
you assign temp malloc'd storage , set temp head - leaking storage (minor).
very minor item - call free(temp)
@ end waste temp null.
Comments
Post a Comment