Code dumps core, why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Code dumps core, why?

I've created code which splits into several threads, when I try to join them together, code returns that it dumped core ( means it tried to access nonallocated memory ) code: https://code.sololearn.com/ca2a7A23a9A1 any nuber bigger than 99 causes error

2nd Jan 2021, 3:25 PM
nicolas turek
nicolas turek - avatar
27 Answers
+ 2
If it is working with lower numbers then it may be that you are exceeding the memory limit on Sololearn. You can try your code with an offline IDE to confirm if it’s Sololearn specific.
2nd Jan 2021, 4:06 PM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 1
the error caused by your code was overflow. Did you write the code yourself? anyway, you could try this solution (I adjusted your code): https://code.sololearn.com/cA7a0a7a20A1 N: B. I tested it on MinGW with C11. Output on my machine: r = 1000 PI = 3.138552000000000 Without threads: 0.004000 PI = 3.138552000000000 With threads: 0.003000
2nd Jan 2021, 5:34 PM
Flash
+ 1
MinGW is for windows, isn't it? by the way, no compiler is able to find atomic
2nd Jan 2021, 5:45 PM
nicolas turek
nicolas turek - avatar
+ 1
i said C11, compile it with C11, it should work on GCC (Linux as well)
2nd Jan 2021, 5:48 PM
Flash
+ 1
when i pasted in your code that you posted above on my ide and it exited with an overflow error. the data type in your loop was int, and I think your sq() is not doing what it should be doing. I used sqrtl() from math.h and long long as data types. In addition, your thread arg struct is messy. and no need to create an array of thread args, you could use just one. also compare your "for" loop in with thread and without thread parts of your solution.
2nd Jan 2021, 6:15 PM
Flash
+ 1
and run your code and see if you get the same or almost the same result as mine, cause I didn't when I ran your code.
2nd Jan 2021, 6:19 PM
Flash
+ 1
i think you should study a bit more threading in C. anyway, I think it's enough for this thread. Good luck!
2nd Jan 2021, 6:22 PM
Flash
+ 1
i don't need to wait for the result, that's why my thread func returns 0 and my resa and resb are atomic, pthread_join() means wait for the thread to finish.
2nd Jan 2021, 6:33 PM
Flash
+ 1
if you want to use another loop, just do it, still, it won't fix your problem.
2nd Jan 2021, 6:42 PM
Flash
+ 1
this will fix my problem, got it. for (int i = 0; i < NUM_OF_THREADS; i++) { pthread_create(threads + i, NULL, threadFunction, (void*)(&args)); } for (int i = 0; i < NUM_OF_THREADS; i++) { pthread_join(threads[i], NULL); } anyway, if my solution seems useless to you better wait for other responses.
2nd Jan 2021, 6:53 PM
Flash
0
no, that doesn't help, it's reason why I have tried it on sololearn, both ends same at input > 99
2nd Jan 2021, 5:33 PM
nicolas turek
nicolas turek - avatar
0
did you run it on your machine? and do you understand threading?
2nd Jan 2021, 5:41 PM
Flash
0
my bad, it will run on sololearn, try now
2nd Jan 2021, 5:46 PM
Flash
0
threading should be something like "splitting" program into multiple processes - not useful on low core cpu, but with 4 will be maximally used with at least 4 threads... so my splitting code into squere of input is not really good... but there should be no problem with it so I gues I understand threading enough to know how to use it, by the way, I had lot of issues when I first time were learning threading in c++ when I were accessing memory at same location because sometimes some threads were "ignored" (they tried to overwrite same place in memory)
2nd Jan 2021, 5:55 PM
nicolas turek
nicolas turek - avatar
0
your version works fine until 10,000
2nd Jan 2021, 5:56 PM
nicolas turek
nicolas turek - avatar
0
btw, in these kinds of calculations, threading may not be a faster option.
2nd Jan 2021, 6:02 PM
Flash
0
yeah, it's slower, I setted my cores to static, it works but this is output: r = 10000 PI = 3.141191 Without threads: 0.002468 PI = 3.100882 With threads: 0.003102
2nd Jan 2021, 6:04 PM
nicolas turek
nicolas turek - avatar
0
and here is mine: r = 10000 PI = 3.141292960000000 Without threads: 0.006000 PI = 3.141292960000000 With threads: 0.006000 Process returned 0 (0x0) execution time : 3.180 s Press any key to continue.
2nd Jan 2021, 6:06 PM
Flash
0
and it's propable that sololearn's compiler is not good for threading because sometimes it works, sometimes returns weird stuff (something around correct result or really much lower) and other times it just dumps core - I'm not sure if threading should be used
2nd Jan 2021, 6:07 PM
nicolas turek
nicolas turek - avatar
0
sololearn didn't invent any compiler btw, it uses GCC on its Linux server. and in your code you had some errors, check them out.
2nd Jan 2021, 6:09 PM
Flash