Segmentation fault. Occurs when assigning an array element, but now in printf

(Update: Problem solved. It boiled down to a stupid typo on my part, which caused me to write the wrong piece of memory, which in turn caused some pointer to the place that was out of order.)

So I'm taking a course that involves some programming and we've essentially been thrown deep into the C pool. I've programmed in other languages ​​before so it's not all new, but I don't have a solid set of tools to debug my code when the proverbial crap gets into the fan.

I essentially had the following

int nParticles = 32;
int nSteps = 10000;
double u[nParticles], v[nParticles];

for (i = 0; i < nSteps; i++) {
...
    for (j = 0; j < nParticles; j++) {
        u[j] = 0.001 * v[j];
    }
...
}

      

as one part of a larger program and I was getting segmentation fault. To identify the problem, I added a bunch of

printf("foo\n");

      

and in the end it turned out that I got to the step i = 209

and the particle j = 31

before the segmentation fault occurred.

After doing a little google search, I realized there a tool called gdb

, and with an additional one printf

there, doing bt

in gdb

, tells me that now it printf

performs a segfault. Mind you, however, I got segfaults before adding diagnostic printfs as well.

It doesn't really matter to me. How can I proceed from here?


Update:

valgrind

gives me the following

==18267== Invalid read of size 8
==18267==    at 0x400EA6: main (in [path redacted])
==18267==  Address 0x7ff001000 is not stack'd, malloc'd or (recently) free'd
==18267== 
==18267== 
==18267== Process terminating with default action of signal 11 (SIGSEGV)
==18267==  Access not within mapped region at address 0x7FF001000
==18267==    at 0x400EA6: main (in [path redacted])
==18267==  If you believe this happened as a result of a stack
==18267==  overflow in your program main thread (unlikely but
==18267==  possible), you can try to increase the size of the
==18267==  main thread stack using the --main-stacksize= flag.
==18267==  The main thread stack size used in this run was 10485760.
==18267== 
==18267== HEAP SUMMARY:
==18267==     in use at exit: 1,136 bytes in 2 blocks
==18267==   total heap usage: 2 allocs, 0 frees, 1,136 bytes allocated
==18267== 
==18267== LEAK SUMMARY:
==18267==    definitely lost: 0 bytes in 0 blocks
==18267==    indirectly lost: 0 bytes in 0 blocks
==18267==      possibly lost: 0 bytes in 0 blocks
==18267==    still reachable: 1,136 bytes in 2 blocks
==18267==         suppressed: 0 bytes in 0 blocks
==18267== Rerun with --leak-check=full to see details of leaked memory
==18267== 
==18267== For counts of detected and suppressed errors, rerun with: -v
==18267== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
Segmentation fault (core dumped)

      

I do not know what it means.


Update:

I tried to comment out the purpose of the array that originally caused the segfault. When I do this, but leave most of the diagnostic printfs, I instead get a segfault in i = 207

.

Update:

The problem has been resolved. In the outer loop (where i

is the counter representing the time steps), I had several inner loops (all of which were reused j

as a counter, iterating over a bunch of particles). However, in one of the inner loops (and not the one that was disabled) I accidentally assigned values E[i]

where E

is an array of size nParticles

, so I went out of bounds. Committing this stops the segfault.

So it all boiled down to a stupid stupid typo on my part.


Update:

I spoke to my brother and he explained the problem in a way that at least suits my limited understanding of the situation.

By accidentally writing things to E

outside of this array, I probably overwrote the pointers associated with my other arrays, and then when I go to access these other arrays, I try to access memory that is not mine and I get a segfault ...

Thank you all for helping me and coming to terms with my lack of knowledge!

+3


source to share


2 answers


It's too long for a comment, but it's not a complete answer. It is impossible to tell why your program dies without seeing a minimal example of code.

printf

usually segfaults for one of three reasons: Either

  • you are passing it a parameter that it cannot access (for example char *

    , which does not point to a properly allocated memory containing a null terminated string), or

  • you have run out of stack space (stack overflow) due to too much recursion, or

  • you've corrupted the heap by writing in excess of dynamic memory allocation.



My advice:

  • Compile your program with the option -Wall

    . Fix each warning and understand why the warning is occurring and not just hiding the problem. Compiling with help -Wall

    is a good habit to get in and catch a lot of errors.

  • Download valgrind

    and run your program in valgrind

    . This will catch a lot of things.

  • Learn to use gdb

    . The tutorial is here .

  • Comment or #if

    from large chunks of your program until you get to the simplest case, which is not an error. Add the bit back and find out what the problem is.

+5


source


See errors like this usually occur when the stack is corrupted.



check if you have an intersection of array boundaries (in particular local arrays)

+3


source







All Articles