Segmentation fault (core dumped) error?

The HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr)) is almost certainly wrong. You want to allocate enough storage for a HashTable, but it seems you're allocating storage for just a pointer to a HashTable(your HashTablePtr ).

Up vote 1 down vote favorite share g+ share fb share tw.

I'm writing a program that uses a Hashtable of linked lists to count the frequency of words. The program will count all the words that I enter along with the frequency however after printing the hashtable, I get a segmentation fault (core dumped) error. When I valgrind my program, it shows that I get errors in three different places that are Invalid read of size 8.

I'm not sure how to fix them though. Here are the three different places: void freeTable(HashTablePtr table) { int i; ListPtr list; if (table == NULL) return; for (i = 0; I size; i++) { list = table->tablei; freeList(list); } free(table->table); free(table); } HashTablePtr createTable(int tableSize) { int i; HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr)); table->table = (ListPtr *) malloc(sizeof(ListPtr) * tableSize); table->size = tableSize; for (i = 0; I size; i++) { table->tablei = createList(); } return table; } void printTable(HashTablePtr table) { ListPtr tempList; NodePtr tempNode; HashObjectPtr obj; int i; for (i = 1; I size; i++) { tempList = table->tablei; if (tempList->size! = 0) { tempNode = tempList->head; obj = tempNode->HashObject; printf("%s\n\n", toString(obj)); } } } I think that the error has to due with using these lines: tempList = table->tablei; table->tablei = createList(); but I'm not sure how to fix it.

Edit: typedef struct hashtable HashTable; typedef struct hashtable * HashTablePtr; struct hashtable { int size; ListPtr *table; }; Valgrind errors: 999 errors in context 5 of 9: ==73795== Invalid read of size 8 ==73795== at 0x400B7D: printTable (HashTable. C:96) ==73795== by 0x400766: main (wf. C:16) ==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd ==73795== at 0x4A0515D: malloc (vg_replace_malloc.

C:195) ==73795== by 0x400D05: createTable (HashTable. C:17) ==73795== by 0x400753: main (wf. C:14) ==73795== ==73795== ==73795== 1000 errors in context 6 of 9: ==73795== Invalid read of size 8 ==73795== at 0x400B2B: freeTable (HashTable.

C:128) ==73795== by 0x40076E: main (wf. C:17) ==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd ==73795== at 0x4A0515D: malloc (vg_replace_malloc. C:195) ==73795== by 0x400D05: createTable (HashTable.

C:17) ==73795== by 0x400753: main (wf. C:14) ==73795== ==73795== ==73795== 1000 errors in context 7 of 9: ==73795== Invalid read of size 8 ==73795== at 0x400D4C: createTable (HashTable. C:25) ==73795== by 0x400753: main (wf.

C:14) ==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd ==73795== at 0x4A0515D: malloc (vg_replace_malloc. C:195) ==73795== by 0x400D05: createTable (HashTable. C:17) ==73795== by 0x400753: main (wf.

C:14) ListPtr createList() { ListPtr list; list = (ListPtr) malloc(sizeof(List)); list->size = 0; list->head = NULL; list->tail = NULL; return list; } c homework segmentation-fault hashtable link|improve this question edited Apr 28 '11 at 18:38 asked Apr 28 '11 at 18:32Kat1359 100% accept rate.

The definition of HashTablePtr and the types of the original valgrind errors would be useful. – Autopulated Apr 28 '11 at 18:35 You should run the program in a debugger. That will tell you in which line the segmentation fault occurs.

– sth Apr 28 '11 at 18:35 I would like to see the impl of createList() – karlphillip Apr 28 '11 at 18:37.

The HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr)); is almost certainly wrong. You want to allocate enough storage for a HashTable, but it seems you're allocating storage for just a pointer to a HashTable(your HashTablePtr) If you drop the habit of typedef'ing pointer and instead follow the approach of allocating in the following form, you won't get into that sort of problems: HashTable *table = malloc(sizeof *table).

I tried this and ran it again and it fixed my problem. And that makes complete sense. Thank you!

– Kat Apr 28 '11 at 18:43 +1 for not typedef'ing pointers. Getting pointers right can be complex. It rarely makes sense to use a typedef to hide the complexity from yourself.

– AShelly Apr 28 '11 at 18:53.

Size of a pointer is always a fixed size and is architecture dependent. On 64-bit platforms, for example, it is always 8 bytes. Basically, sizeof (ObjectType) is not the same as sizeof (ObjectType *).

So in this case you end up allocating less memory than you need which leads to segmentation faults.

Does not do what you think it does. It parses like this.

My program compiles fines but upon inputting a file I get a "Segmentation fault (core dumped)" error. Am I not handling the ostream correctly? #include struct Reading { int hour; double temperature; Reading(int h, double t): hour(h), temperature(t) { } bool operator get_temps(); double check_adjust_temp(double temperature, char scale); double c_to_f(double temperature); double mean(vector temps); double median(vector temps); void print_results(const vector& temps, double mean_temp, double median_temp); int main() try { vector temps = get_temps(); if (temps.size() == 0) error("no temperatures given!

"); double mean_temp = mean(temps); sort(temps.begin(), temps.end()); double median_temp = median(temps); print_results(temps, mean_temp, median_temp); } catch (exception& e) { cerr get_temps() { cout > name; ifstream ist(name. C_str()); if(!ist) error("can't open input file ", name); vector temps; int hour; double temperature; while (ist >> hour >> temperature){ if (hour temps) { double mean_temp; double sum = 0; for (int I = 0; I temps) { double median_temp; sort (temps.begin(), temps.end()); median_temp = tempstemps.size()/2. Temperature; return (median_temp); } void print_results(const vector& temps, double mean_temp, double median_temp) { cout \n"; cout.

You might have better luck if you tell us where the segfault occurs, your debugger should be able to tell you. – mu is too short Dec 6 '10 at 4:14.

Scale == 'c' || 'C' does not do what you think it does. It parses like this: ( scale == 'c' ) || 'C' and 'C' is always true. If you had compiler warnings enabled, you should have noticed this.

(No, this is not your immediate problem; it's up at the end of get_temps. But with warnings enabled, you would have seen that too. ).

Sorry my compiler isn't catching anything, I can't quite get warnings to work properly. Could you elaborate more on the problem at the end of get_temps? Thanks.

– John Dec 6 '10 at 4:49 All non-void functions must have a return value. (Okay, there's exceptions to that rule, but they don't apply here. ) – ephemient Dec 6 '10 at 4:54 @John: The same with Reading::operator The body of that function should be a one-liner: return temperature It really should be warning you about all this.

– Prætorian Dec 6 '10 at 5:49.

Attach a debugger and step through your code. It's possible you are dividing by 0 or running past the end of an array. At the very least we need more info before we can help.

If so, Valgrind is a great tool for diagnosing segmentation fault errors.

If it's not throwing an exception, then code that calls it will keep going with bogus data. Cin >> name; only reads a "word" from the standard input, not a whole line. So if there are spaces in the file name, your program won't get the correct file name.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions