Why does malloc segfault




















Any chance of running the program under valgrind? If memory corruption is going on, valgrind might be able to show you where and when. Show 19 more comments. Active Oldest Votes. Moreover I see that you never free the memory here and there can be a possible memory leak.

Improve this answer. Jekyll Jekyll 1, 10 10 silver badges 13 13 bronze badges. The problem is that the segmentation fault only happens after a lot of calls. I didn't see any free here I will run into a problem if I don't free memory sooner or later This is just a minimal model, in the original version it doesn't have memory leaks. So in this version i only want to find the malloc problem.

If you guess it's a malloc problem you should just allocate and free to avoid going out of heap memory tousands of time malloc doesn't know your structure , so you don't need the full program, but this is very unlikely to happen guilhermemtr — Jekyll.

Show 10 more comments. I've figured out what the problem is: a stack overflow. Thank you all! If it wasn't your help i wouldn't be able to figure it out! That was what I was guessing as I couldn't find any problem. But to ensure this is the problem can you dump the 'top' output on a file and check how the memory usage increase? I've set the ulimit bigger and i could then run bigger fib nums. But i'll do as you said, just to confirm — guilhermemtr. Add a comment.

Sign up or log in Sign up using Google. Sign up using Facebook. Usually it is the limit on stack size that causes this kind of problem.

To check memory limits, use the ulimit command in bash or ksh , or the limit command in csh or tcsh. Try setting the stacksize higher, and then re-run your program to see if the segfault goes away. If you can't find the problem any other way, you might try a debugger. For example, you could use GNU's well-known debugger GDB to view the backtrace of a core file dumped by your program; whenever programs segfault, they usually dump the content of their section of the memory at the time of the crash into a core file.

Start your debugger with the command gdb core , and then use the backtrace command to see where the program was when it crashed. This simple trick will allow you to focus on that part of the code. If using backtrace on the core g file doesn't find the problem, you might have to run the program under debugger control, and then step through the code one function, or one source code line, at a time.

To do this, you will need to compile your code without optimization, and with the -g flag, so information about source code lines will be embedded in the executable file. This is document aqsj in the Knowledge Base. Last modified on Barring problems from elsewhere in the code, malloc will only do one of two things: return a pointer to allocated space, or return a null pointer because it couldn't allocate.

Simple as that. DEV Community is a community of , amazing developers We're a place where coders share, stay up-to-date and grow their careers. Create account Log in. Twitter Facebook Github Instagram Twitch. Collapse Expand Stolaine Stolaine Stolaine.



0コメント

  • 1000 / 1000