+ 1
C - "dumped core" error
Consider these codes (the Python and C codes which show the letters having the maximum frequency in the sentence): https://code.sololearn.com/ch4DVBoK5P30/?ref=app https://code.sololearn.com/c1AfV8ch4Ok4/?ref=app The input "cheddar cheese cheddar cheese" works in python but not in C. Why is this so? Is C good for data-handling? Which one of my codes is faster? The only advantage I can find with C is that it is memory efficient.
8 Answers
+ 2
The error is on line 14 :
realloc() does NOT reallocate the memory block in-place. It returns the address to the reallocated block. So even though you are calling realloc() on line 14, `a` still holds the same memory block and possibly (and worse), a deallocated block (because if I'm right, realloc() might deallocate the old block and reallocate at a totally different location).
You need to assign the returned pointer to `a`. So the if-condition on line 14 needs to be changed to
```
a = (char*) realloc(a, b + 1);
if (a == NULL) ....
```
OR
`if ((a = (char*)realloc(a, b + 1)) == NULL) ...`
Making this change fixes the code.
The same problem is there on line 39 also, but for some reason, that is not affecting the code. But I suggest you fix that too, because even if it's not causing error, it is causing memory leaks and illegal memory access.
"Which one of my codes is faster?"
Although C is faster, I believe your C code in its current form is slower than the Python code. But it's only a speculation.
+ 2
Also, you have overcomplicated the code. I think that is reason for the bugs in your code. Frankly, your code is so complicated that I couldn't even read till the end. I have no idea what you're doing on lines 17-61.
Here is my solution. I believe this one is faster than the Python code, but then again, it is only a speculation.
https://code.sololearn.com/cwgt1DXm21c5/?ref=app
+ 1
Calvin Thomas
It was in the back of my mind, but I forgot to tell in my answer that your progam was encountering a memory leak (you forgot to deallocate the allocated memory). But apprently, you noticed that, and now it's fine.
The comments help. Also, for sorting, there is already a function qsort() in <stdlib.h> which can be used.
Even now, I believe the Python code will be faster. You code has a lot of realloc() operations, and also a lot of for-loops, which become more and more expensive as the data gets bigger.
"From what I understand....."
Yes, you are right.
+ 1
XXX Thank you very much!
+ 1
Well,you should name you code as letter frequency rather word freq.
0
XXX The 2 bugs (actually 3, including the probable realloc bug) have been solved and the code works fine now. I've also added comments to explain everything in my code. Thanks for the response. Now, which one is faster (executed in a computer)?
From what I understand from your comments, when a contiguous block of memory to be allocated isn't available with the present pointer, the present one is freed and a new pointer is stored in place of the first one. Is this interpretation correct?
0
000 I didn't notice that when naming the code. Thanks for the correction.
0
As for "the monitored program dumped core" message, that means that the development environment you're using ran your program, but your program crashed. That, in turn, means that your program has a bug. You need to get out the debugger and run the program in the debugger to find out why it is crashing.