-4

When running my c code I keep recieving the error message Segmentation Fault (core dumped)

That is all I receive. Is there a way I can see more from this error? I.E. through a c library at the top of the c code?

Vatine
  • 20,782
  • 4
  • 54
  • 70
  • 1
    What about some code to see what happens? – Fernando Gallego Fernández Nov 10 '15 at 10:20
  • 6
    Other than using a debugger? – Ignacio Vazquez-Abrams Nov 10 '15 at 10:20
  • 5
    try `gdb` --> https://www.gnu.org/software/gdb/ – Haris Nov 10 '15 at 10:21
  • 1
    race-condition relates to this how? – Sami Kuhmonen Nov 10 '15 at 10:25
  • And your code is ? You should check everything you code for errors and you'll see that it is easier to spot seg faults. – Michi Nov 10 '15 at 10:25
  • 1
    @Michi, It is kind of hard to spot for beginners without debuggers. – Haris Nov 10 '15 at 10:27
  • @Haris Please explain how can a beginner understand a debugger if he/she has problems with the language ? Do debuggers explain them all this ? :) – Michi Nov 10 '15 at 10:29
  • @Michi, So you're saying a programmer who is an expert, doesn't need a debugger to spot *seg faults*, and a programmer who is a beginner cannot use one? :D – Haris Nov 10 '15 at 10:30
  • @Haris No, I mean that a beginner (we speak about beginners) should learn the language first and then (only then) debuggers. Or if he/she can, to learn them both but is like learning piano from a video tutorial you have to feel it to, if you know what I mean. :D – Michi Nov 10 '15 at 10:32
  • 1
    Whatever: without real C code to see, only general answer can be done. And it is: use a debugger and/or check your code and/or add explicit debug info in your code. – hexasoft Nov 10 '15 at 10:33
  • @Michi, unproductive argument.. Lets drop it.. – Haris Nov 10 '15 at 10:34
  • 1
    If you cannot debug at all, you cannot develop software and should not try to write it. Writing code, geting it to build, finding it does not work as required and then immediately posting it here is just insulting and annoying. – Martin James Nov 10 '15 at 10:36
  • 1
    https://wiki.archlinux.org/index.php/Step-by-step_debugging_guide and http://stackoverflow.com/questions/2876357/determine-the-line-of-c-code-that-causes-a-segmentation-fault are amongst the top google hits. – tripleee Nov 10 '15 at 10:36
  • @Haris I was prepared [to give the following example](http://pastebin.com/raw.php?i=xLwT94S6), but i found my self with the closed topic. – Michi Nov 10 '15 at 11:14

2 Answers2

2

You can use a debugger to find the line that causes this error. That should be enough to solve your problem. Segmentation fault is often caused by accessing a memory location that is not available. For example, consider following code:

int array[5];
array[2] = 10; // OK
array[20] = 10; // Segmentation fault

This commonly occurs when you make a mistake in writing a loop.

See this question for an example how to debug your program with GDB:

First compile your program:

gcc program.c -g -o program

Then use gdb:

gdb ./program
(gdb) run
<segfault happens here>
(gdb) backtrace
<offending code is shown here>
Community
  • 1
  • 1
VLL
  • 9,634
  • 1
  • 29
  • 54
1

The message core dumped means that a core-file has been created. A core-file is a file which contains all content of the memory, related to the process which has just crashed (core dumps typically are created when an application crashes).

There are two things you can do: you can look for the cause, by looking into your program when this happened, or you can investigate the core dump, in order to understand which kind of error has led to this situation. In most cases, this can be done by reading the call stack from your core dump. The core dump can be located anywhere, I have known situations where it was created in the runtime directory of the running process, I have known situations where core dumps where automatically moved to /var/core. About applications for reading core dumps, I have worked with dbx and gdb, but I think that ladebug might be useful too.

I know, I give plenty of new questions, but I hope that now you have an idea in which directions to look for further information.

Dominique
  • 16,450
  • 15
  • 56
  • 112